修改以允许快速排序到包含重复元素的数组

时间:2016-07-21 08:33:39

标签: java quicksort

下面的代码适用于具有不同数字的数组,但对于包含重复项的数组则失败。如何修改下面的代码甚至可以用于包含重复项的数组?

import java.util.Random;

public class QuickSortNew {
    public static void main(String[] args) {
//      int[] a = {1,4,10,3,8,7,5,2,6,9};
//      int[] a = {1,49,30,15,27,21,1,20,1,39};
//      int[] a = {33, 47, 4, 9, 27, 16, 3, 10, 4, 43};
//      int[] a = {3, 1, 4, 1, 10, 9, 74, 16, 99, 27, 31, 43, 28, 33, 47};
        int[] a = {3, 4, 4};

//      int[] a = new int[10];
//      populate(a);
//      print(a);
//      System.out.println("");
        fun(a, 0, a.length-1);
        print(a);
    }

    static void fun(int[] a, int l, int h)
    {
        if(l >= h)
            return;

        int pivot = a[(l+h)/2];
        int startl = l;
        int starth = h;
        while(l < h)
        {
            while(a[l] < pivot)
                l++;
            while(a[h] > pivot)
                h--;

            if(l < h)
            {
                int t = a[l];
                a[l] = a[h];
                a[h] = t;
            }
        }
        fun(a, startl, l-1);
        fun(a, l+1, starth);
    }

    static void populate(int[] a)
    {
        Random rand = new Random();
        for(int i = 0;i<10;i++)
            a[i] = rand.nextInt(50);
    }

    static void print(int[] a)
    {
        for(int i = 0 ;i<a.length;i++)
            System.out.print(a[i] + " ");
    }
}

我搜索了一下,发现下面的代码。但是,我不想这样做如果(i&lt; = j) {没有立即理解,因为如果i == j,那应该是枢轴,你只想修复那个位置,而不是再继续这个过程。相反,请建议对上面的代码进行不同的更改。

private void quickSort(int lowerIndex, int higherIndex) {

        int i = lowerIndex;
        int j = higherIndex;
        // calculate pivot number, I am taking pivot as middle index number
        int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
        // Divide into two arrays
        while (i <= j) {
            /**
             * In each iteration, we will identify a number from left side which
             * is greater then the pivot value, and also we will identify a number
             * from right side which is less then the pivot value. Once the search
             * is done, then we exchange both numbers.
             */
            while (array[i] < pivot) {
                i++;
            }
            while (array[j] > pivot) {
                j--;
            }
            if (i <= j) {
                System.out.println("i=="+i+",j=="+j+",entering inside the condition...");
                exchangeNumbers(i, j);
                //move index to next position on both sides
                i++;
                j--;
            }
        }
        // call quickSort() method recursively
        if (lowerIndex < j)
            quickSort(lowerIndex, j);
        if (i < higherIndex)
            quickSort(i, higherIndex);
    }

    private void exchangeNumbers(int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
编辑:能够解决它。对我来说,下面的实现比在线广泛使用的实现更有意义。根据我的说法,如果前后指针相同,遍历应该停止,因为我们有一个阵列,其中前半部分小于,而后半部分高于枢轴。我不喜欢上面的实现,因为即使i == j。

它们也会继续
import java.util.Arrays;
import java.util.Random;

public class QuickSortNew {
    public static void main(String[] args) {
//      int[] a = {1,4,10,3,8,7,5,2,6,9};
//      int[] a = {1,49,30,15,27,21,1,20,1,39};
//      int[] a = {33, 47, 4, 9, 27, 16, 3, 10, 4, 43};
//      int[] a = {3, 1, 4, 1, 10, 9, 74, 16, 99, 27, 31, 43, 28, 33, 47};
//      int[] a = {3, 4, 4};
//      int[] a = {1,2,7,6,10,11,4,12,13,5,14,15,8,3,9};
//      int[] a = {1,7,6,10,11,4,12,13};
//      int[] a = {3, 1, 4, 1, 10, 9, 74, 16, 99};

        Random rand = new Random();
        int n = rand.nextInt(50)+1;
        int[] a = new int[n];
        int[] b = new int[n];
        populate(n,a);
        for(int i = 0;i<a.length;i++)
            b[i] = a[i];
        System.out.print("Original array : ");
        print(b);
        System.out.println("");
        fun(a, 0, a.length-1);
        System.out.print("My sort     : ");
        print(a);
        Arrays.sort(b);
        System.out.println("");
        System.out.print("Library sort: ");
        print(b);
        System.out.println("");
        System.out.println("Is my sort equal to library sort ? "+Arrays.equals(a, b));
    }

    static void fun(int[] a, int l, int h)
    {
        if(l >= h)
            return;

        int pivot = a[(l+h)/2];
        int pivotpos = (l+h)/2;
        int startl = l;
        int starth = h;
        while(l < h)
        {
            while(a[l] <= pivot && pivotpos!=l )
                l++;
            while(a[h] >= pivot && pivotpos!=h )
                h--;

            if(l < h)
            {
                int t = a[l];
                a[l] = a[h];
                a[h] = t;

                if(l==pivotpos)
                    pivotpos = h;
                else if(h == pivotpos)
                    pivotpos = l;
            }
        }
        fun(a, startl, l-1);
        fun(a, l+1, starth);
    }

    static void populate(int n, int[] a)
    {
        Random rand = new Random();
        for(int i = 0;i<n;i++)
            a[i] = rand.nextInt(50);
    }

    static void print(int[] a)
    {
        for(int i = 0 ;i<a.length;i++)
            System.out.print(a[i] + " ");
    }
}

0 个答案:

没有答案