Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的快速排序实现_Java_Algorithm_Quicksort - Fatal编程技术网

Java中的快速排序实现

Java中的快速排序实现,java,algorithm,quicksort,Java,Algorithm,Quicksort,我试图实现快速排序,但它不能正常工作 请告诉我哪里出错了。我是否错误地实现了逻辑? 我用这些数字集测试了上述代码-13,26,12,15,10,15,12 public class QuickSort { private int array[]; private int arrayLength; public void sort(int[] values) { if (values == null || values.length == 0)

我试图实现快速排序,但它不能正常工作

请告诉我哪里出错了。我是否错误地实现了逻辑? 我用这些数字集测试了上述代码-13,26,12,15,10,15,12

public class QuickSort {

    private int array[];
    private int arrayLength;

    public void sort(int[] values) {

        if (values == null || values.length == 0)
            return;

        this.array = values;
        this.arrayLength = array.length - 1;

        quickSort(0, arrayLength);
    }

    private void quickSort(int low, int high) {
        int i = low, j = high;

            // Maximum Number of elements should be equal to arraylength
            int leftSubArray[] = new int[high+1];
            int rightSubArray[] = new int[high+1];

            int pivot = array[low];
            System.out.println("Pivot = " + pivot + " and position = " + low);

            int tempMax = low;

            // Divide the list in two parts

            // Left sublist smaller than pivot
            // Incremented by one to exclude the pivot
            while (i < high) {
                if (array[i + 1] < pivot) {
                    leftSubArray[tempMax] = array[i + 1];
                    tempMax++;
                }
                i++;
            }

            // Right sublist greater than pivot
            tempMax = j;
            while (j > low) {
                if (array[j] >= pivot) {
                    rightSubArray[tempMax] = array[j];
                    tempMax--;
                }
                j--;
            }

            // Combining both the arrays
            i = low;
            while (i <= tempMax) {
                array[i] = leftSubArray[i];
                i++;
            }

            array[tempMax] = pivot;

            // defining the limit of the next recursive call
            j = tempMax-1;
            i = low;

            tempMax++;
            while (tempMax <= high) {
                array[tempMax] = rightSubArray[tempMax];
                tempMax++;
            }

            displayArray();

            // Recursion
            if (low < j) 
                quickSort(low, j);

            tempMax++;
            if (tempMax < arrayLength)
                quickSort(tempMax, arrayLength);

    }

    private void displayArray() {
        for (int i : array) {
            System.out.print(i + ",");
        }
        System.out.println("\b\n");
    }
}

以下是工作代码

public class QuickSort{

int arr[] = {12,9,4,99,120,1,3,130,13};


public static void main(String args[])
{

QuickSort qs = new QuickSort();

qs.quickSort(qs.arr,0,qs.arr.length-1);
System.out.println("");


}

void quickSort(int arr[],int left,int right)
{
   int i = left, j = right;

   int tmp;int p;

   int pivot = arr[(left + right) / 2];


System.out.println("");

for(p=0;p<arr.length;p++)
{
    System.out.print(arr[p] + " ");

}System.out.println("\n\nPivot = " +pivot+" Left= "+left+" j= " +j+ " I= "+i+ " Right= "+right+"  {before entering do-while}\n");

/* partition */

  while (i <= j) {

        while (arr[i] < pivot)

              i++;

        while (arr[j] > pivot)

              j--;

        if (i <= j) {

              tmp = arr[i];

              arr[i] = arr[j];

              arr[j] = tmp;

              i++;

              j--;

        }
/*for(p=0;p<arr.length;p++)
    {
        System.out.print(arr[p]+" ");

    }
    System.out.println();*/

  }




for(p=0;p<arr.length;p++)
{
    System.out.print(arr[p]+" ");
}

System.out.println("\n\nPivot = " +pivot+" Left= "+left+" j= " +j+ " I= "+i+ " Right= "+right+" {after each do-while}");

/***********/


 /* recursion */

  if (left < j){
    System.out.println("\nInside First if Left = "+left+ " J = " +j);       

        quickSort(arr, left, j);
}

  if (i < right){
    System.out.println("\nInside Second if i = " +i+ " Right = " +right);
        quickSort(arr, i, right);
}

/*******/
公共类快速排序{
int arr[]={12,9,4,99120,1,3130,13};
公共静态void main(字符串参数[])
{
快速排序qs=新的快速排序();
qs.quickSort(qs.arr,0,qs.arr.length-1);
System.out.println(“”);
}
无效快速排序(int-arr[],int-left,int-right)
{
int i=左,j=右;
int-tmp;
int pivot=arr[(左+右)/2];
System.out.println(“”);

对于(p=0;p在’s和’s评论的指导下,我能够正确地实现逻辑。

您的测试结果是什么?“它工作不正常”不是对任何问题的充分描述。它会崩溃吗?产生错误的结果吗?到底是什么错了?很奇怪,你有一个外循环。基本的快速排序算法是:分区,递归,递归。我建议你在使事情变得更复杂之前先把它工作起来。@abc“分区”指围绕枢轴对阵列进行分区。这是一个通常在单独函数中推送的操作,正如TheodoreNorvell指出的那样,递归调用。由于分区通常在适当的位置实现,您不需要创建新阵列。没问题,很高兴我能帮上忙。感谢您的代码,但我想更正我编写的代码我知道这是不正确的,这就是为什么不能正常工作的原因…但我对你的建议投了赞成票answer@pise这个问题看起来像是一个家庭作业问题。在这种情况下,我会避免给出有效的解决方案,因为最好是通过理解和纠正自己的错误来学习,而不是看解决方案。@GiovanniBotta我会用谷歌搜索和直接从那里获取代码,但我尝试自己实现它,因此发布了问题。停止评判我!@abc如果我似乎是评判性的,我很抱歉,这不是我的意图。此外,我是回应pise建议帮助解决问题,而不是给出解决方案。正如您也注意到的,您希望修复代码并取消理解它为什么不起作用,这绝对是公平合理的。我只是想用“像作业一样”的问题来说明这一点。
public class QuickSort{

int arr[] = {12,9,4,99,120,1,3,130,13};


public static void main(String args[])
{

QuickSort qs = new QuickSort();

qs.quickSort(qs.arr,0,qs.arr.length-1);
System.out.println("");


}

void quickSort(int arr[],int left,int right)
{
   int i = left, j = right;

   int tmp;int p;

   int pivot = arr[(left + right) / 2];


System.out.println("");

for(p=0;p<arr.length;p++)
{
    System.out.print(arr[p] + " ");

}System.out.println("\n\nPivot = " +pivot+" Left= "+left+" j= " +j+ " I= "+i+ " Right= "+right+"  {before entering do-while}\n");

/* partition */

  while (i <= j) {

        while (arr[i] < pivot)

              i++;

        while (arr[j] > pivot)

              j--;

        if (i <= j) {

              tmp = arr[i];

              arr[i] = arr[j];

              arr[j] = tmp;

              i++;

              j--;

        }
/*for(p=0;p<arr.length;p++)
    {
        System.out.print(arr[p]+" ");

    }
    System.out.println();*/

  }




for(p=0;p<arr.length;p++)
{
    System.out.print(arr[p]+" ");
}

System.out.println("\n\nPivot = " +pivot+" Left= "+left+" j= " +j+ " I= "+i+ " Right= "+right+" {after each do-while}");

/***********/


 /* recursion */

  if (left < j){
    System.out.println("\nInside First if Left = "+left+ " J = " +j);       

        quickSort(arr, left, j);
}

  if (i < right){
    System.out.println("\nInside Second if i = " +i+ " Right = " +right);
        quickSort(arr, i, right);
}

/*******/