Java 快速排序:维基百科实现不起作用

Java 快速排序:维基百科实现不起作用,java,algorithm,sorting,quicksort,Java,Algorithm,Sorting,Quicksort,我刚刚尝试从wikipedia()实现快速排序算法,但是缺少了一些东西。这是我的密码: public static void quicksort(int[] a, int lo, int hi) { if(lo < hi) { int p = partition(a, lo, hi); quicksort(a, lo, p - 1); quicksort(a, p + 1, hi); } } public static in

我刚刚尝试从wikipedia()实现快速排序算法,但是缺少了一些东西。这是我的密码:

public static void quicksort(int[] a, int lo, int hi) {
    if(lo < hi) {
        int p = partition(a, lo, hi);
        quicksort(a, lo, p - 1);
        quicksort(a, p + 1, hi);
    }
}

public static int partition(int[] a, int lo, int hi) {
    //choose pivot element
    int pivotIndex = 0; 
    int pivotVal = a[pivotIndex];

    //put pivot at the end of array
    swap(a, pivotIndex, hi);

    //compare other elements to pivot and swap accordingly
    int storeindex = lo;
    for(int i = lo; i < hi; i++) {
        if(a[i] <= pivotVal) {
            swap(a, i, storeindex);
            storeindex++;
        }
        //set pivot in right place of array
        swap(a, storeindex, hi);
    }

    return storeindex; //return 
}

public static void swap(int[] a, int place1, int place2) {
    int temp = a[place1];
    a[place1] = a[place2];
    a[place2] = temp;
}
返回:
1,2,3,5,4
而不是它应该:
1,2,3,4,5

我已经盯着这个看了很长一段时间了,如果有人能帮我找出哪里出了问题,我将不胜感激:)谢谢

两个问题:

  • 您需要从分区中选择pivot值,而不仅仅是获取数组的第一个元素

  • 最后一次交换应该在循环之外,您将在遍历分区后将pivot元素放置到它的位置。请参见最后两行:

  • 固定分区方法:

    public static int partition(int[] a, int lo, int hi) {
        //choose pivot element
        int pivotIndex = lo; // ERROR 1 fixed
        int pivotVal = a[pivotIndex];
    
        //put pivot at the end of array
        swap(a, pivotIndex, hi);
    
        //compare other elements to pivot and swap accordingly
        int storeindex = lo;
        for (int i = lo; i < hi; i++) {
            if (a[i] <= pivotVal) {
                swap(a, i, storeindex);
                storeindex++;
            }
        }
    
        //set pivot in right place of array
        swap(a, storeindex, hi); // ERROR 2 fixed
        return storeindex; //return 
    }
    
    公共静态int分区(int[]a,int-lo,int-hi){
    //选择轴元素
    int pivotIndex=lo;//错误1已修复
    int pivotVal=a[pivotIndex];
    //将枢轴放在数组的末尾
    交换(a,数据透视索引,hi);
    //将其他元素与轴心进行比较并相应地交换
    int-storeindex=lo;
    对于(int i=lo;i如果(a[i]选择数据透视本身,则始终使用分区的第一个或最后一个元素作为数据透视将意味着排序/接近排序的数组(如您正在使用的数据)的最差性能。
    
    public static int partition(int[] a, int lo, int hi) {
        //choose pivot element
        int pivotIndex = lo; // ERROR 1 fixed
        int pivotVal = a[pivotIndex];
    
        //put pivot at the end of array
        swap(a, pivotIndex, hi);
    
        //compare other elements to pivot and swap accordingly
        int storeindex = lo;
        for (int i = lo; i < hi; i++) {
            if (a[i] <= pivotVal) {
                swap(a, i, storeindex);
                storeindex++;
            }
        }
    
        //set pivot in right place of array
        swap(a, storeindex, hi); // ERROR 2 fixed
        return storeindex; //return 
    }