Java 快速排序不起作用
在运行上述代码时,第一个枢轴返回为3。在第一个递归方法中,此枢轴作为2传输,但在第二个递归中,它不取值4。有人能找出问题所在吗Java 快速排序不起作用,java,Java,在运行上述代码时,第一个枢轴返回为3。在第一个递归方法中,此枢轴作为2传输,但在第二个递归中,它不取值4。有人能找出问题所在吗 class QuickSortRevision{ int pivot; void QuickSort(int[] arr,int low,int high){ if(low>=high) return; pivot = quickSortPivot(arr,low,high);//first
class QuickSortRevision{
int pivot;
void QuickSort(int[] arr,int low,int high){
if(low>=high)
return;
pivot = quickSortPivot(arr,low,high);//first execution pivot =3
QuickSort(arr,low,pivot-1);//this is taking 0,2 as parameter;
QuickSort(arr,pivot+1,high);//but this is not taking 4,8 as parameter;
}
int quickSortPivot(int[] arr,int low,int high){
int temp,index,partition,lindex,hindex;
lindex=low;
hindex=high-1;
partition = arr[high];
index=high;
System.out.println("low : "+low+" high "+high);
while(lindex!=hindex){
while(arr[lindex]<partition && (lindex!=hindex) ){
lindex++;
}
while(arr[hindex]>partition && (lindex!=hindex) ){
hindex--;
}
if( lindex!=hindex)
{
temp=arr[lindex];
arr[lindex]=arr[hindex];
arr[hindex]=temp;
lindex++;hindex--;
}
}
temp=arr[lindex];
arr[lindex]=partition;
arr[index]=temp;
System.out.println("lindex: "+lindex);
return lindex;
}
void printArray(int[] arr)
{
for(int element : arr)
System.out.print(" "+element);
}
public static void main(String[] args){
QuickSortRevision qs = new QuickSortRevision();
int arr[]={17,41,5,22,54,6,29,3,13};
qs.QuickSort(arr,0,arr.length-1);
qs.printArray(arr);
}}
类QuickSortRevision{
int轴;
无效快速排序(int[]arr、int低、int高){
如果(低>=高)
返回;
pivot=QuickPortPivot(arr、低、高);//第一次执行pivot=3
快速排序(arr,low,pivot-1);//这将0,2作为参数;
快速排序(arr,pivot+1,high);//但这不是将4,8作为参数;
}
int quickSortPivot(int[]arr、int低、int高){
int-temp、index、partition、lindex、hindex;
lindex=低;
hindex=高-1;
分区=arr[高];
指数=高;
系统输出打印项次(“低:“+低+”高“+高);
while(lindex!=hindex){
while(arr[lindex]分区&(lindex!=hindex)){
印地语--;
}
如果(lindex!=hindex)
{
温度=arr[lindex];
arr[lindex]=arr[hindex];
arr[印地语]=温度;
lindex++;hindex--;
}
}
温度=arr[lindex];
arr[lindex]=分区;
arr[指数]=温度;
System.out.println(“lindex:+lindex”);
返回林黛克斯;
}
无效打印数组(int[]arr)
{
for(int元素:arr)
系统输出打印(“+元素);
}
公共静态void main(字符串[]args){
QuickSortRevision qs=新QuickSortRevision();
int arr[]={17,41,5,22,54,6,29,3,13};
快速排序(arr,0,arr.length-1);
打印阵列(arr);
}}
第一次调用QuickSort时,类成员pivot
的值为3。然后,对QuickSort的递归调用调用quickSortPivot,其结果被分配给pivot
(进一步的递归调用也会修改此值)。当此快速排序调用返回时,pivot
的值已被修改
您应该将pivot
定义为方法QuickSort
的变量,而不是类QuickSortRevision
PS:第一次调用QuickSort时,应调用函数
QuickSort
类成员pivot
的值为3。然后,对QuickSort的递归调用调用quickSortPivot,其结果被分配给pivot
(进一步的递归调用也会修改此值)。当此快速排序调用返回时,pivot
的值已被修改
您应该将pivot
定义为方法QuickSort
的变量,而不是类QuickSortRevision
PS:应该调用
快速排序函数快速排序替换您的代码lindex=hindex
至lindex hindex
。
代码如下所示:
public class QuickSortRevision{
int pivot;
static int id = 1;
void QuickSort(int[] arr,int low,int high){
if(low>=high)
return;
pivot = quickSortPivot(arr,low,high);//first execution pivot =3
QuickSort(arr,low,pivot-1);//this is taking 0,2 as parameter;
QuickSort(arr,pivot+1,high);//but this is not taking 4,8 as parameter;
}
int quickSortPivot(int[] arr,int low,int high){
int temp,index,partition,lindex,hindex;
lindex=low;
hindex=high - 1;
partition = arr[high];
index=high;
while(lindex <= hindex){
while(arr[lindex]<partition && (lindex<=hindex) ){
lindex++;
}
while(arr[hindex]>partition && (lindex<=hindex) ){
hindex--;
}
System.out.println("low : "+low+" high "+high);
if( lindex<=hindex)
{
temp=arr[lindex];
arr[lindex]=arr[hindex];
arr[hindex]=temp;
lindex++;hindex--;
}
}
temp=arr[lindex];
arr[lindex]=partition;
arr[index]=temp;
// System.out.println(lindex+" "+arr[lindex]);
System.out.println("lindex: "+lindex);
return lindex;
}
void printArray(int[] arr)
{
for(int element : arr)
System.out.print(" "+element);
}
public static void main(String[] args){
QuickSortRevision qs = new QuickSortRevision();
int arr[]={17,41,5,22,54,6,29,3,13};
qs.QuickSort(arr,0,arr.length-1);
qs.printArray(arr);
}
公共类QuickSortRevision{
int轴;
静态int-id=1;
无效快速排序(int[]arr、int低、int高){
如果(低>=高)
返回;
pivot=QuickPortPivot(arr、低、高);//第一次执行pivot=3
快速排序(arr,low,pivot-1);//这将0,2作为参数;
快速排序(arr,pivot+1,high);//但这不是将4,8作为参数;
}
int quickSortPivot(int[]arr、int低、int高){
int-temp、index、partition、lindex、hindex;
lindex=低;
hindex=高-1;
分区=arr[高];
指数=高;
而(lindex将您的代码lindex!=hindex
替换为lindex-hindex
)。
代码如下所示:
public class QuickSortRevision{
int pivot;
static int id = 1;
void QuickSort(int[] arr,int low,int high){
if(low>=high)
return;
pivot = quickSortPivot(arr,low,high);//first execution pivot =3
QuickSort(arr,low,pivot-1);//this is taking 0,2 as parameter;
QuickSort(arr,pivot+1,high);//but this is not taking 4,8 as parameter;
}
int quickSortPivot(int[] arr,int low,int high){
int temp,index,partition,lindex,hindex;
lindex=low;
hindex=high - 1;
partition = arr[high];
index=high;
while(lindex <= hindex){
while(arr[lindex]<partition && (lindex<=hindex) ){
lindex++;
}
while(arr[hindex]>partition && (lindex<=hindex) ){
hindex--;
}
System.out.println("low : "+low+" high "+high);
if( lindex<=hindex)
{
temp=arr[lindex];
arr[lindex]=arr[hindex];
arr[hindex]=temp;
lindex++;hindex--;
}
}
temp=arr[lindex];
arr[lindex]=partition;
arr[index]=temp;
// System.out.println(lindex+" "+arr[lindex]);
System.out.println("lindex: "+lindex);
return lindex;
}
void printArray(int[] arr)
{
for(int element : arr)
System.out.print(" "+element);
}
public static void main(String[] args){
QuickSortRevision qs = new QuickSortRevision();
int arr[]={17,41,5,22,54,6,29,3,13};
qs.QuickSort(arr,0,arr.length-1);
qs.printArray(arr);
}
公共类QuickSortRevision{
int轴;
静态int-id=1;
无效快速排序(int[]arr、int低、int高){
如果(低>=高)
返回;
pivot=QuickPortPivot(arr、低、高);//第一次执行pivot=3
快速排序(arr,low,pivot-1);//这将0,2作为参数;
快速排序(arr,pivot+1,high);//但这不是将4,8作为参数;
}
int quickSortPivot(int[]arr、int低、int高){
int-temp、index、partition、lindex、hindex;
lindex=低;
hindex=高-1;
分区=arr[高];
指数=高;
虽然(lindex您应该提供有关错误或出错原因的详细信息,确切地说,堆栈
溢出中的堆栈
跟踪在哪里?问题在于pivot被声明为类变量而不是函数变量,因此递归无法保留值。但是有什么区别吗?即使我们将pivot声明为函数variable它仍然会被修改,不会被修改。每次调用函数时,都会在内存中分配一个新的int。这个int是这个函数调用的本地int,这意味着如果你递归调用你的函数,它将使用自己版本的pivot,当它返回时,你的pivot不会被修改。谢谢StephaneM。真的很感激你我们的快速回复。您应该提供有关错误或错误的详细信息,确切地说,堆栈
溢出中的堆栈
跟踪在哪里?问题在于pivot被声明为类变量而不是函数变量,因此递归无法保留值。但是有什么区别吗?即使我们将pivot声明为func变量仍然会被修改,不会被修改。每次调用函数时,都会在内存中分配一个新的int。这个int是这个函数调用的本地int,这意味着如果递归调用函数,它将使用自己版本的pivot,当它返回时,pivot不会被修改。谢谢StephaneM。非常感谢请快速回复。是的,这解决了问题。但我仍然不明白为什么pivot必须在QuickSort函数内部声明,而不是外部声明。即使我们将pivot声明为函数变量,它仍然会被修改,不是吗。是的,这是