Java无法搞清楚Quicksort Median of 3

时间:2016-12-01 02:43:15

标签: java sorting quicksort

我很难做Quicksort Median of 3(截止日期)。在此之后我必须弄清楚5的中位数,但是我需要弄清楚我在这里做错了什么因为我得到了一个未分类的答案。

    public class QuicksortTest1 
    {   
        public static <AnyType extends Comparable<? super AnyType>> void    
        quicksort(AnyType [] a)
        {
            quicksort(a, 0, a.length - 1);
        }   

        private static <AnyType extends Comparable<? super AnyType>> void   
        quicksort(AnyType [] a, int left, int right)
        {
            final int CUTOFF = 3;

            if (left + CUTOFF <= right) 
            { 
                AnyType pivot = (AnyType) median3 (a, left, right);

                int i = left, j = right - 1;
                for( ; ;)
            {
                while( a[++i].compareTo(pivot) < 0) {}
                while( a[--j].compareTo(pivot) < 0) {}
                if(i < j)
                    swap(a, i, j);
                else
                    break;
        }
            swap(a, i, right - 1);

            quicksort(a, left, i - 1);
            quicksort(a, i + 1, right);
        } 
        else
            insertionSort(a, left, right);
    } 

    private static <AnyType extends Comparable<? super AnyType>> AnyType 
    median3(AnyType [] a, int left, int right)
    {
        int center = (left + right) /2;

        if (a[center].compareTo(a[left]) < 0)
            swap(a,left,center);

        if (a[right].compareTo(a[left]) < 0)
            swap(a, left, right);

        if (a[right].compareTo(a[center]) < 0)
            swap(a, center, right);

        swap(a, center, right - 1);
        return a[right - 1]; 
    }

    private static void swap(Comparable a[], int i, int j)
    {
        Comparable tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }

    private static void insertionSort(Comparable a[], int left, int right)
    {
        int j;

        for (int p = left + 1; p <= right; p++)
        {
            Comparable tmp = a[p];
            for (j = p; j > left && tmp.compareTo( a[j - 1]) < 0; j--)
                a[j] = a[j-1];
                a[j] = tmp;
    }
  } 

    public static void main (String [] args)
    {
        Comparable [] a = {1,53,86,21,49,32,90,65,33,11,
                      34,68,54,32,78,80,35,22,96,59,265,44324,123,3123,25435};
        quicksort(a);
        for (int i = 0; i < a.length; i++)
            System.out.print(a[i] + " ");        
    } 

}

//结果:1 49 265 32 44324 21 53 123 11 3123 25435 33 34 32 59 96 22 86 90 35 65 68 54 78 80

1 个答案:

答案 0 :(得分:0)

对评论中注明的代码的更改

    private static <AnyType extends Comparable<? super AnyType>> void   
    quicksort(AnyType [] a, int left, int right)
    {
        final int CUTOFF = 3;
        if (left + CUTOFF <= right) 
        { 
            AnyType pivot = (AnyType) median3 (a, left, right);
            int i = left - 1, j = right + 1;    // fix
            for( ; ;)
            {
                while( a[++i].compareTo(pivot) < 0) {}
                while( a[--j].compareTo(pivot) > 0) {} // fix
                if(i < j)
                    swap(a, i, j);
                else
                    break;
            }
        //  swap(a, i, right - 1);              // delete
            quicksort(a, left, j);              // fix
            quicksort(a, j + 1, right);         // fix
        } 
          else
            insertionSort(a, left, right);
    } 

    private static <AnyType extends Comparable<? super AnyType>> AnyType 
    median3(AnyType [] a, int left, int right)
    {
        int center = (left + right) /2;
        if (a[center].compareTo(a[left]) < 0)
            swap(a,left,center);
        if (a[right].compareTo(a[left]) < 0)
            swap(a, left, right);
        if (a[right].compareTo(a[center]) < 0)
            swap(a, center, right);
    //  swap(a, center, right - 1);             // delete
        return a[center];                       // fix
    }