java 原始快速排序算法引发堆栈溢出异常:在分区方法中查找主元的下一个位置

hyrbngr7  于 2023-05-27  发布在  Java
关注(0)|答案(1)|浏览(109)

嘿,这是我的快速排序代码,它适用于正常情况,但不适用于数组具有相同元素的情况,在这种情况下,我考虑将等于pivot的元素放在pivot的左侧。有人能纠正我代码中的逻辑错误吗?

public class Solution {

    public static int partition(int input[],int si, int ei) {
        int pivot = input[si],pivotPos = si,count = 0;
        // counting no of elements less than pivot so that  space can be left empty to left of pivot
        for(int i = pivotPos+1; i<input.length;i++) {
            if(input[i]<=pivot)
              count++;
        }
        //swapping pivot with the element
        input[si] = input[si+count];
        input[si+count] = pivot;
        pivotPos = si+count;
        // ensuring all elements to the left of pivot are less and to the right are more
        int i = si, j = ei,temp;
       
       while(i<pivotPos && j>pivotPos) {
        if(input[i]<=pivot)
        i++;
        else  {
            if(input[j]>pivot)
            j--;
            else {
                temp = input[i];
                input[i] = input[j];
                input[j] = temp;
                i++;
                j--;
            }
         }
       }
       return pivotPos;
    }

    public static void quickSort(int input[], int si, int ei) {
        if(si>=ei)
        return; 
        int pivotPos =  partition(input,si,ei);
        quickSort(input,si,pivotPos-1);
        quickSort(input,pivotPos+1,ei);
    }
    
    public static void quickSort(int[] input) {
        /* Your class should be named Solution
         * Don't write main().
         * Don't read input, it is passed as function argument.
         * No need to print or return the output.
         * Taking input and printing output is handled automatically.
         */
         quickSort(input,0,input.length - 1);
        
    }
    
}

输入测试用例:4 3 8 4 6 5预期输出:三四四五六八
我的代码从分区函数被调用的点给出堆栈溢出错误

ukdjmx9f

ukdjmx9f1#

我想我已经修复了你的代码,同时对你的代码风格做了最小的修改。你会注意到我留下了一些你的代码,即使这不是我们真正需要做的。我把它留在这里供你比较。

public class QuickSort {

    public static int partition(int input[],int si, int ei) {
        int pivot = input[si],pivotPos = si,count = 0;
        // counting no of elements less than pivot so that  space can be left empty to left of pivot

        for(int i = pivotPos+1; i<input.length;i++) {
            if(input[i]<=pivot) {
                count++;
            }
        }
        // the above var count is not the info we need
        // we need to know how many elements between si and ei (inclusive)
        // are less than the value of pivot

        // corrected logic here:
        int smallerThanPivotCount = 0;
        for (int i = si; i <= ei; i++) { // note that our logic includes ei
            if (input[i] < pivot)
                smallerThanPivotCount++;
        }

        //swapping pivot with the element
        input[si] = input[si + smallerThanPivotCount];
        input[si + smallerThanPivotCount] = pivot;
        pivotPos = si + smallerThanPivotCount;

        // next we are
        // ensuring all elements to the left of pivot are less and to the right are more
        int i = si, j = ei,temp;

        while(i<pivotPos && j>pivotPos) {
            if(input[i]<=pivot)
                i++;
            else  {
                if(input[j]>=pivot)
                    j--;
                else {
                    temp = input[i];
                    input[i] = input[j];
                    input[j] = temp;
                    i++;
                    j--;
                }
            }
        }
        return pivotPos;
    }

    public static void quickSort(int input[], int si, int ei) {
        if(si>=ei)
            return;
        int pivotPos =  partition(input,si,ei);
        quickSort(input,si,pivotPos - 1);
        quickSort(input,pivotPos+1,ei);
    }

    public static void quickSort(int[] input) {
        /* Your class should be named Solution
         * Don't write main().
         * Don't read input, it is passed as function argument.
         * No need to print or return the output.
         * Taking input and printing output is handled automatically.
         */
        quickSort(input,0,input.length - 1);
    }

}

我认为你的问题只是你没有正确计算你需要移动轴的空格数。我写了代码。我不能保证它100%正确,但它确实通过了我列出的单元测试,包括您自己的示例数组。我建议你自己检查一下,以理解其中的逻辑。然后把多余的东西清理干净,让它成为你自己的。干杯

相关问题