Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 修改以允许对包含重复元素的数组进行快速排序_Java_Quicksort - Fatal编程技术网

Java 修改以允许对包含重复元素的数组进行快速排序

Java 修改以允许对包含重复元素的数组进行快速排序,java,quicksort,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}

我下面的代码适用于具有不同数字的数组,但对于包含重复项的数组则失败。如何修改下面的代码,使其即使在包含重复项的数组中也能工作

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] + " ");
    }
}
import java.util.Random;
公共类QuickSortNew{
公共静态void main(字符串[]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=新的int[10];
//(a);
//印刷品(a);
//System.out.println(“”);
乐趣(a,0,a.length-1);
印刷品(a);
}
静态虚空乐趣(int[]a,int l,int h)
{
如果(l>=h)
返回;
int-pivot=a[(l+h)/2];
int START=l;
int starth=h;
而(l枢轴)
h--;
if(lwhile(数组[i]如何
while(数组[i]
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;
    }
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] + " ");
    }
}