简体   繁体   中英

Java having trouble figuring out Quicksort Median of 3

I am having a hard time doing Quicksort Median of 3 (with a cutoff). I have to figure out Median of 5 after this but I need to figure out what I am doing wrong here because I am getting an unsorted answer.

    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] + " ");        
    } 

}

//result: 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

Changes to code noted in comments

    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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM