快速排序(随机透视)代码未正确排序Java
该代码是一个带有随机轴的快速排序算法。它没有对随机数组进行完全排序;我不明白为什么。错误似乎与递归调用和交换的界限有关快速排序(随机透视)代码未正确排序Java,java,quicksort,Java,Quicksort,该代码是一个带有随机轴的快速排序算法。它没有对随机数组进行完全排序;我不明白为什么。错误似乎与递归调用和交换的界限有关 public static <E extends Comparable<E>> void quickerSort(ArrayList<E> input, int lower, int higher){ int first = lower; int last = highe
public static <E extends Comparable<E>> void quickerSort(ArrayList<E> input, int lower, int higher){
int first = lower;
int last = higher;
if (lower >= higher)
return;
//generates the random pivot
int pivot = rand.nextInt(higher-lower+1) + lower;
//moves the pivot to the beginning of the list
E temp = input.get(first);
input.set(first, input.get(pivot));
input.set(pivot, temp);
pivot = first;
first++;
//if the values on the left side of the array are less than the
//value of the pivot, first is incremented (first part of loop) until
//a greater value is reached
//if the values on the right side of the array are greater than the
//value of the pivot, last is incremented until a lesser value is reached
while (first < last){
while (input.get(first).compareTo(input.get(pivot)) <= 0 && first < last){
first++;
}
while(input.get(last).compareTo(input.get(pivot)) > 0 && last >= first){
last--;
}
//switches the two values reached through the while loops
if (first < last){
temp = input.get(first);
input.set(first, input.get(last));
input.set(last, temp);
}
}
//moves the pivot to where first is
temp = input.get(first-1);
input.set(first-1, input.get(pivot));
input.set(pivot, temp);
//calls the method recursively
if (lower < first-1){
quickerSort(input,lower,first-1);
}
if (first < higher){
quickerSort(input,first,higher);
}
}
publicstaticvoidquickersort(arraylistinput,int-lower,int-higher){
int first=较低;
int last=更高;
如果(较低>=较高)
返回;
//生成随机轴
int pivot=rand.nextInt(较高-较低+1)+较低;
//将轴移动到列表的开头
E temp=输入。获取(第一);
input.set(首先,input.get(pivot));
输入设置(枢轴、温度);
枢轴=第一;
第一++;
//如果数组左侧的值小于
//第一个轴的值递增(循环的第一部分),直到
//达到更大的值
//如果数组右侧的值大于
//轴的值last递增,直到达到较小的值
while(第一次<最后一次){
while(input.get(first).compareTo(input.get(pivot))0&&last>=first){
最后--;
}
//切换通过while循环达到的两个值
如果(第一次<最后一次){
temp=输入。获取(第一);
input.set(第一个,input.get(最后一个));
输入。设置(最后一个,温度);
}
}
//将轴移动到第一个轴所在的位置
温度=输入。获取(第一个-1);
input.set(first-1,input.get(pivot));
输入设置(枢轴、温度);
//递归调用该方法
如果(降低<第一个-1){
快速排序(输入,降低,第一个-1);
}
如果(第一个<更高){
快速排序(输入,第一,更高);
}
}
//实施方法
public static void testQuickSort(){
//creates a random list of integers to be sorted and prints it
ArrayList<Integer> list = new ArrayList<Integer>();
Random rand = new Random();
for (int i = 0; i <= 19; i++){
list.add(rand.nextInt(100));
System.out.print(list.get(i)+" ");
}
//calls quicksort and then prints the sorted array
System.out.println();
Assignment2.quickSort(list);
for (int i = 0; i <= 19; i++){
System.out.print(list.get(i)+" ");
}
}
publicstaticvoidtestquicksort(){
//创建要排序的整数的随机列表并打印它
ArrayList=新建ArrayList();
Random rand=新的Random();
对于(int i=0;i您的代码对于一个简单的输入{6,0}不起作用,因为您的first
和last
都将指向第二个元素,因此,6和0永远不会被交换
我对你的代码做了一点修改,它起了作用。我使用了变量名,这在这里更有意义,但逻辑是一样的
public static <E extends Comparable<E>> void quickSort(ArrayList<E> input, int low, int high) {
if (low >= high)
return;
// generates the random pivot
int pivotIndex = new Random().nextInt(high - low + 1) + low;
E pivot = input.get(pivotIndex);
// moves the pivot to the beginning of the list
Collections.swap(input, pivotIndex, low);
int i = low;
int j = high;
// if the values on the left side of the array are less than the
// value of the pivot, first is incremented (first part of loop) until
// a greater value is reached
// if the values on the right side of the array are greater than the
// value of the pivot, last is incremented until a lesser value is
// reached
while (i <= j) {
while (input.get(i).compareTo(pivot) <= 0 && i < j) {
i++;
}
while (input.get(j).compareTo(pivot) > 0 && j >= i) {
j--;
}
// break here when two pointers meet since the pivot position has been found
if (i >= j)
break;
// switches the two values reached through the while loops
Collections.swap(input, i, j);
}
// moves the pivot to where j is
Collections.swap(input, low, j);
// calls the method recursively
quickSort(input, low, j - 1);
quickSort(input, j + 1, high);
}
publicstaticvoidquicksort(arraylistinput,int-low,int-high){
如果(低>=高)
返回;
//生成随机轴
int pivotIndex=new Random().nextInt(高-低+1)+低;
E pivot=input.get(pivotIndex);
//将轴移动到列表的开头
集合.交换(输入,数据透视索引,低位);
int i=低;
int j=高;
//如果数组左侧的值小于
//第一个轴的值递增(循环的第一部分),直到
//达到更大的值
//如果数组右侧的值大于
//轴的值last将递增,直到生成较小的值
//达到
while(i=i){
j--;
}
//找到枢轴位置后,当两个指针相遇时在此中断
如果(i>=j)
打破
//切换通过while循环达到的两个值
集合。交换(输入,i,j);
}
//将轴移动到j所在的位置
集合。交换(输入,低,j);
//递归调用该方法
快速排序(输入,低,j-1);
快速排序(输入,j+1,高);
}
您的代码对于简单的输入{6,0}不起作用,因为您的first
和last
都将指向第二个元素,因此,6和0永远不会被交换
我对你的代码做了一点修改,它起了作用。我使用了变量名,这在这里更有意义,但逻辑是一样的
public static <E extends Comparable<E>> void quickSort(ArrayList<E> input, int low, int high) {
if (low >= high)
return;
// generates the random pivot
int pivotIndex = new Random().nextInt(high - low + 1) + low;
E pivot = input.get(pivotIndex);
// moves the pivot to the beginning of the list
Collections.swap(input, pivotIndex, low);
int i = low;
int j = high;
// if the values on the left side of the array are less than the
// value of the pivot, first is incremented (first part of loop) until
// a greater value is reached
// if the values on the right side of the array are greater than the
// value of the pivot, last is incremented until a lesser value is
// reached
while (i <= j) {
while (input.get(i).compareTo(pivot) <= 0 && i < j) {
i++;
}
while (input.get(j).compareTo(pivot) > 0 && j >= i) {
j--;
}
// break here when two pointers meet since the pivot position has been found
if (i >= j)
break;
// switches the two values reached through the while loops
Collections.swap(input, i, j);
}
// moves the pivot to where j is
Collections.swap(input, low, j);
// calls the method recursively
quickSort(input, low, j - 1);
quickSort(input, j + 1, high);
}
publicstaticvoidquicksort(arraylistinput,int-low,int-high){
如果(低>=高)
返回;
//生成随机轴
int pivotIndex=new Random().nextInt(高-低+1)+低;
E pivot=input.get(pivotIndex);
//将轴移动到列表的开头
集合.交换(输入,数据透视索引,低位);
int i=低;
int j=高;
//如果数组左侧的值小于
//第一个轴的值递增(循环的第一部分),直到
//达到更大的值
//如果数组右侧的值大于
//轴的值last将递增,直到生成较小的值
//达到
while(i=i){
j--;
}
//找到枢轴位置后,当两个指针相遇时在此中断
如果(i>=j)
打破
//切换通过while循环达到的两个值
集合。交换(输入,i,j);
}
//将轴移动到j所在的位置
集合。交换(输入,低,j);
//递归调用该方法
快速排序(输入,低,j-1);
快速排序(输入,j+1,高);
}
你试过了吗?现在是星期五。更严重的是,一些纸张和调试器可能有助于找出答案。你试过了吗?现在是星期五。更严重的是,一些纸张和调试器可能有助于找出答案