如何在Java中正确实现快速排序的递归?
我目前正在用Java进行快速排序,我已经成功地为第一次迭代排序了列表。尽管如此,我的递归实现并不是我想要的。原因是什么 列表是[11,4,53,65,44,23,202,37,1] 以下是按Java升序正确实现的快速排序如何在Java中正确实现快速排序的递归?,java,algorithm,sorting,recursion,quicksort,Java,Algorithm,Sorting,Recursion,Quicksort,我目前正在用Java进行快速排序,我已经成功地为第一次迭代排序了列表。尽管如此,我的递归实现并不是我想要的。原因是什么 列表是[11,4,53,65,44,23,202,37,1] 以下是按Java升序正确实现的快速排序 public static List<Integer> quickSort(List<Integer> l1, int from, int to) { System.out.println("Quick Sort \n");
public static List<Integer> quickSort(List<Integer> l1, int from, int to) {
System.out.println("Quick Sort \n");
long startTime = System.nanoTime();
//select a pivot - the last element of the list
int pivot = l1.get(to - 1);
//introduce two counters:
int counterLastSwapPos = 0;//this first one will track the index of the element
//that is bigger than the pivot - we start from zero (we never actually
// know that this number is actually bigger - it is a
// presupposition)
for (int counter = 0; counter < l1.indexOf(pivot); counter++) {
//we also have a counter to track our position during the iteration
//if the element at the current position is smaller than the pivot
//swap the element(current position) with the element that is bigger
//than the pivot.
if (l1.get(counter) < pivot) {
int temp = l1.get(counter);
l1.set(counter, l1.get(counterLastSwapPos));
l1.set(counterLastSwapPos, temp);
//Once the swap has happened - increment the counter
//that tracks the number bigger than the pivot
counterLastSwapPos++;
//finally, in the loop, the position counter will be
//automatically incremented
}
//when the position counter reaches the last allowed position,
//swap the pivot with the the counter that tracks
// the number bigger than the pivot
if (counter == l1.indexOf(pivot) - 1) {
l1.set(l1.indexOf(pivot), l1.get(counterLastSwapPos));
l1.set(counterLastSwapPos, pivot);
}
}
//as this sorting is a "Divide&Conquer" type, we use recursion to perform
//the same operations on two parts of the list. That is why, (if you scroll up),
//you'll see that once the list becomes size of 1, the recursion will stop.
//Our pivot is now somewhere in the middle - this was our aim.
//Now, pay attention to perform the recursion on
//two lists that WILL NOT include the pivot itself
if (from < l1.indexOf(pivot)) quickSort(l1, from, l1.indexOf(pivot));
if (l1.indexOf(pivot) + 1 < to) quickSort(l1, l1.indexOf(pivot) + 1, to);
//list is sorted
long endTime = System.nanoTime();
long duration = (endTime - startTime);
System.out.println("Time: " + duration + "\n");
return l1;
}
顺便说一下,你要么随意选择你的支点,要么把它放在中间和右边,而不是把它放在最后。to@user真正地关于这个解释视频,你能说些什么?@用户我已经试过使用子列表了,但是我如何将它们加在一起呢?我只想操纵原始列表,这样我就不必像合并排序那样考虑如何连接排序后的列表。例如,我不确定,但就我所见,该视频似乎没有使用与您相同的算法。看这个:你是什么意思?在这儿吗?quickSortl1,0,l1.indexOfpivot;quickSortl1,l1.indexOfpivot+1,l1.size;如果是,请执行此quickSortl1,from,l1.indexOfpivot;快速入门l1,l1.indexOfpivot+1,至;
public static List<Integer> quickSort(List<Integer> l1, int from, int to) {
System.out.println("Quick Sort \n");
long startTime = System.nanoTime();
//select a pivot - the last element of the list
int pivot = l1.get(to - 1);
//introduce two counters:
int counterLastSwapPos = 0;//this first one will track the index of the element
//that is bigger than the pivot - we start from zero (we never actually
// know that this number is actually bigger - it is a
// presupposition)
for (int counter = 0; counter < l1.indexOf(pivot); counter++) {
//we also have a counter to track our position during the iteration
//if the element at the current position is smaller than the pivot
//swap the element(current position) with the element that is bigger
//than the pivot.
if (l1.get(counter) < pivot) {
int temp = l1.get(counter);
l1.set(counter, l1.get(counterLastSwapPos));
l1.set(counterLastSwapPos, temp);
//Once the swap has happened - increment the counter
//that tracks the number bigger than the pivot
counterLastSwapPos++;
//finally, in the loop, the position counter will be
//automatically incremented
}
//when the position counter reaches the last allowed position,
//swap the pivot with the the counter that tracks
// the number bigger than the pivot
if (counter == l1.indexOf(pivot) - 1) {
l1.set(l1.indexOf(pivot), l1.get(counterLastSwapPos));
l1.set(counterLastSwapPos, pivot);
}
}
//as this sorting is a "Divide&Conquer" type, we use recursion to perform
//the same operations on two parts of the list. That is why, (if you scroll up),
//you'll see that once the list becomes size of 1, the recursion will stop.
//Our pivot is now somewhere in the middle - this was our aim.
//Now, pay attention to perform the recursion on
//two lists that WILL NOT include the pivot itself
if (from < l1.indexOf(pivot)) quickSort(l1, from, l1.indexOf(pivot));
if (l1.indexOf(pivot) + 1 < to) quickSort(l1, l1.indexOf(pivot) + 1, to);
//list is sorted
long endTime = System.nanoTime();
long duration = (endTime - startTime);
System.out.println("Time: " + duration + "\n");
return l1;
}