快速排序中的逻辑错误,中位数为3

指令是编辑一个快速排序程序,以选择三个中位数作为枢轴,而不是数组的第一个值。

为此,我对代码进行了如下编辑:

public class medianOf3 {

        //Main method and test array 
        public static void main(String[] args) {

            int[] list = {2,3,2,5,6,1,-2,14,12};
            quickSort(list);
            for (int i = 0; i < list.length; i++)
                System.out.print(list[i] + " ");
            System.out.println();
        }

        public static void quickSort(int[] list) {
            quickSort(list,list.length - 1);
        }
        //The quicksort method 
        public static void quickSort(int[] list,int first,int last) {
            if (last > first) {
                int pivotIndex = partition(list,first,last);
                quickSort(list,pivotIndex - 1);
                quickSort(list,pivotIndex + 1,last);
            }
        }

       // Returns the median of three integers 
        public static int median(int first,int middle,int last) {
            return Math.max(Math.min(first,middle),Math.min(Math.max(first,last));
        }

        //returns the index of a value 
        public static int  findIndex (int[] list,int t) { 
            if (list == null) return -1;
            int len = list.length;
            int i = 0;
            while (i < len) {
                if (list[i] == t) return i;
                else i=i+1;
            }
            return -1;
        }

    public static int partition(int[] list,int last) {

        int middle = ((last-first) / 2)+first;    
        int pivot = median(list[first],list[middle],list[last]); // selecting the median of three (of the first,middle and last value) as the pivot
        int low = first +1; // Index for forward search
        int high = last; // Index for backward search
        int index = findIndex(list,pivot );

        int swap = list[index];
        list[index] = list[0];
        list[0] = swap;

        while (high > low) {

            // Search forward from left
            while (low <= high && list[low] <= pivot)
            low++;
            // Search backward from right
            while (low <= high && list[high] > pivot)
            high--;
            // Swap two elements in the list
                if (high > low) {
                int temp = list[high];
                list[high] = list[low];
                list[low] = temp;
                }
            }

        while (high > first && list[high] >= pivot)
        high--;

        // Swap pivot with list[high]

        if (pivot > list[high]) {
            list[first] = list[high];
            list[high] = pivot;
            return high;
        } else { return first;}
        }  

}

上面的代码返回以下输出:

14 1 2 2 3 3 5 6 12 14

所需的输出是这样:

-2 1 2 2 3 3 5 6 12 14

使用调试器,我可以对要正确排序的数组进行一次计算, 只需交换最后两个值:

-2 1 2 2 3 3 5 6 14 12

的最终发行内
list[index] = list[0];
使用调试器,分区方法的

行是发生错误的地方。我觉得这很可能是一个逻辑错误,但我不确定当时到底出了什么问题。

感谢所有反馈。

wjr349464977 回答:快速排序中的逻辑错误,中位数为3

我建议作为解决方案(根据您的代码进行一些更改):

public static void main(String[] args) {
    List<Integer> list = Arrays.asList(2,3,2,5,6,1,-2,14,12);
    quickSort(list,list.size() - 1);
    System.out.println(list);
}

private static void quickSort(List<Integer> list,int first,int last) {
    if (last - first > 0) {
        int pivot = pivot(list,first,last);
        int index = partition(list,last,pivot);
        quickSort(list,index - 1);
        quickSort(list,index + 1,last);
    }
}

private static int pivot(List<Integer> list,int last) {
    return ((last - first) / 2) + first;
}

private static int partition(List<Integer> list,int last,int pivot) {
    Collections.swap(list,pivot);
    int j = first;
    for (int i = first; i < last; i++) {
        if (list.get(i) <= list.get(last)) {
            Collections.swap(list,i,j);
            j++;
        }
    }
    Collections.swap(list,j);
    return j;
}
,

具有单个功能和防止堆栈溢出的替代示例(最坏情况下时间复杂度仍为O(n ^ 2))。在此示例中,通过对a [lo,md,hi]排序来执行3的中值,其中md =(lo + hi)/ 2,这需要3次if / swaps。

    @SuppressWarnings("empty-statement")
    public static void qsort(int[] a,int lo,int hi)
    {
        while(lo < hi){
            int  md = lo+(hi-lo)/2;
            int  ll = lo-1;
            int  hh = hi+1;
            int t;
            if(a[lo] >  a[hi]){         // median of 3
                t     = a[lo];
                a[lo] = a[hi];
                a[hi] = t;
            }
            if(a[lo] >  a[md]){
                t     = a[lo];
                a[lo] = a[md];
                a[md] = t;
            }
            if(a[md] >  a[hi]){
                t     = a[md];
                a[md] = a[hi];
                a[hi] = t;
            }
            int p = a[md];
            while(true){                // partition
                while(a[++ll] < p);
                while(a[--hh] > p);
                if(ll >= hh)
                    break;
                t     = a[ll];
                a[ll] = a[hh];
                a[hh] = t;
            }
            ll = hh++;
            // recurse on smaller part,loop on larger part
            if((ll - lo) <= (hi - hh)){
                qsort(a,lo,ll);
                lo = hh;
            } else {
                qsort(a,hh,hi);
                hi = ll;
            }
        }
    }
本文链接:https://www.f2er.com/3084795.html

大家都在问