Java 快速排序故障
我正在进行快速排序,但由于某些原因,我最终遇到了相同的问题。 不知何故,即使在复制/粘贴本应有效的快速排序代码时,我始终会在数组中的某个位置插入一个值0Java 快速排序故障,java,quicksort,Java,Quicksort,我正在进行快速排序,但由于某些原因,我最终遇到了相同的问题。 不知何故,即使在复制/粘贴本应有效的快速排序代码时,我始终会在数组中的某个位置插入一个值0 public class quicksort { public int[] x; public quicksort(int a[]){ x=a; procedure(0, x.length-1); } public void procedure(int left, int right){ int index=left;
public class quicksort {
public int[] x;
public quicksort(int a[]){
x=a;
procedure(0, x.length-1);
}
public void procedure(int left, int right){
int index=left;
int smaller=left+1;//going to sort everything but the first element (the pivot or index)
int larger=right;
int divider=x[index];
while (smaller <= larger){
for (int z=0; z<x.length-1;z++){//display the array at each pass
System.out.print(x[z]+" ,");
}
System.out.println("|| "+smaller+" "+divider+" " +larger+" ||");
while(x[larger] > divider ){ //stops at smaller then divider coming from the right
larger--;
}
while(x[smaller] < divider){//stops at bigger then divider coming from the left
smaller++;
}
if (smaller<=larger){//swaps two elements
swap(smaller, larger);
smaller++;
larger--;
}
}
index= smaller + insert(left, smaller);//moves index to its final spot in the array
//recursive call, split the array by the position of index and sort each
if (index < right-1)
procedure(index+1, right);
if (index > left+1)
procedure(left, index-1);
}
public void swap(int z, int y){//swaps values between 2 array indexes
int temp;
temp =x[z];
x[z]=x[y];
x[y]=temp;
}
public int insert(int z, int y){//inserts a value from one index to the another index in the array, adjusts array as neccessary
int it=0;
if (x[z]>x[y]){ //if values are the same => easy swap
swap(z,y);
it=0;
} else if (x[z] <= x[y]){ //if trying to insert to a bigger value
for (int f =z; f>y-1;f++) //will swap values from the position z to
swap(f, f+1); //position y (only works if z<y)
it=-1;
}
return it;
}
}
首先,这段代码看起来非常复杂,就像快速排序一样。看起来您想要实现Lomuto版本,但代码中至少有一些不寻常的地方 对于自己实现快速排序,我强烈建议阅读有关Lomuto快速排序的内容,并了解其实现的典型方式。在该版本中,您将获得: ->函数
partition
(它获取数组,选择一个轴,然后返回一个索引,在该索引处,轴在所有元素轴都位于其右侧后插入)
->函数quicksort
,将递归调用该函数对轴左侧和轴右侧的部分数组进行排序
洛穆托快速排序通常被认为比C.A.R.霍尔设计的原始快速排序更容易理解。但是,您也可以尝试使用Hoare的快速排序来获得更小的代码
void quickSort(int left, int right, int tab[]){
int i = left;
int j = right;
int buffer;
int pivot = tab[(i+j)/2];
do{
while (tab[i] < pivot) i++;
while (tab[j] > pivot) j--;
if (i <= j){
buffer = tab[i]; //swap.
tab[i] = tab[j];
tab[j] = buffer;
i++;
j--;
}
}
while (i <= j);
if (left < j) qs(left, j, tab);
if (i < right) qs(i, right, tab);
}
void快速排序(int-left、int-right、int-tab[]){
int i=左;
int j=右;
int缓冲区;
int pivot=tab[(i+j)/2];
做{
而(tab[i]pivot)j--;
如果(我感谢是的,你是对的,我把它复杂化了,在总体思路如此简单的情况下迷失在自己的复杂中。再次感谢。放松点,重新阅读quicksort的主要思想,看看一些实现,然后自己做,就这么简单。记住,如果你发现答案有帮助,请将其标记为接受。
void quickSort(int left, int right, int tab[]){
int i = left;
int j = right;
int buffer;
int pivot = tab[(i+j)/2];
do{
while (tab[i] < pivot) i++;
while (tab[j] > pivot) j--;
if (i <= j){
buffer = tab[i]; //swap.
tab[i] = tab[j];
tab[j] = buffer;
i++;
j--;
}
}
while (i <= j);
if (left < j) qs(left, j, tab);
if (i < right) qs(i, right, tab);
}