J'ai fait de la pratique avec des algorithmes de tri et je continue à obtenir ce error pour mon tri de fusion. Je ne peux même pas faire défiler vers le haut pour voir ce que l'erreur d'origine était parce qu'il affiche à SortTestPractice.mergesort etc. tellement de fois j'atteins la limite supérieure sur l'affichage. Des idées sur ce que l'erreur pourrait être?erreur récursive mergesort
Voici mon code pour le morceau en 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;
}
La ligne il me pointe est left = mergeSort(left);
. Ma première pensée était qu'elle ne reconnaissait pas quand la gauche et la droite ne contenaient qu'une seule donnée et continuaient à essayer de les diviser. Je suis venu avec une solution rapide pour cela en ajoutant une instruction if else, mais j'ai toujours la même erreur.
ici est ma fonction de fusion
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);
}
}
}
À quoi ressemble votre fonction de fusion comme? – Steven
J'ai ajouté la fonction de fusion –
Quelle est la ligne en question? – Avery246813579