[英]Recursive mergesort error
I've been doing some practice with sorting algorithms and I keep getting this error for my merge sort. 我一直在使用排序算法进行练习,并且在合并排序时不断遇到此错误 。 I can't even scroll up to see what the original error was because it displays at SortTestPractice.mergesort etc. so many times I reach the upper limit on display. 我什至无法滚动查看原始错误是什么,因为它在SortTestPractice.mergesort等处显示,所以我多次达到显示上限。 Any ideas as to what the error could be? 关于错误可能是什么的任何想法?
Here is my code for the chunk in question 这是我有关代码的代码
public static ArrayList<String> mergeSort(ArrayList<String> testList)
{
ArrayList<String> left = new ArrayList<String>();
ArrayList<String> right = new ArrayList<String>();
int center;
center = testList.size()/2;
// copy the left half of testList into the left.
for (int i=0; i<center; i++) {
left.add(testList.get(i));
}
//copy the right half of testList into the new arraylist.
for (int i=center; i<testList.size(); i++) {
right.add(testList.get(i));
}
// Sort the left and right halves of the arraylist.
left = mergeSort(left);
right = mergeSort(right);
// Merge the results back together.
merge(left, right, testList);
return testList;
}
The line it points me to is left = mergeSort(left);
它指向我的left = mergeSort(left);
线是left = mergeSort(left);
. 。 My initial thought was that it wasn't recognizing when left and right contained only 1 piece of data and kept trying to split them. 我最初的想法是,它无法识别左和右何时仅包含1个数据,并一直试图对其进行拆分。 I came up with a quick fix for that by adding an if else statement but I still got the same error. 通过添加if else语句,我想出了一个快速解决方案,但仍然遇到相同的错误。
here is my merge function 这是我的合并功能
private static ArrayList<String> merge(ArrayList<String> left, ArrayList<String> right, ArrayList<String> testList) {
// source: modified from http://www.codexpedia.com/java/java-merge-sort-implementation/
int leftIndex = 0;
int rightIndex = 0;
int testListIndex = 0;
// As long as neither the left nor the right ArrayList has
// been used up, keep taking the smaller of left.get(leftIndex)
// or right.get(rightIndex) and adding it at both.get(bothIndex).
while (leftIndex < left.size() && rightIndex < right.size()) {
if ( (left.get(leftIndex).compareTo(right.get(rightIndex))) < 0) {
testList.set(testListIndex, left.get(leftIndex));
leftIndex++;
} else {
testList.set(testListIndex, right.get(rightIndex));
rightIndex++;
}
testListIndex++;
}
ArrayList<String> rest;
int restIndex;
if (leftIndex >= left.size()) {
// The left ArrayList has been use up...
rest = right;
restIndex = rightIndex;
} else {
// The right ArrayList has been used up...
rest = left;
restIndex = leftIndex;
}
// Copy the rest of whichever ArrayList (left or right) was not used up.
for (int i=restIndex; i<rest.size(); i++) {
testList.set(testListIndex, rest.get(i));
testListIndex++;
}
return testList;
}
public static void hybridSort(ArrayList<String> testList)
{
int temp = 0;
for (int i = 0; i<100; i++)
{
if (testList.get(i).compareTo(testList.get(i+1)) < 0)
{
temp++;
}
}
if (temp >= 90)
{
insertionSort(testList);
}
else
{
mergeSort(testList);
}
}
}
Add this to the beginning of mergeSort function. 将此添加到mergeSort函数的开头。 You need a base case. 您需要一个基本案例。
if(testList.size() <= 1){ return testList; }
--- edit @rcgldr commented the same before i did ---编辑@rcgldr在我做之前发表了同样的评论
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.