Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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_Sorting_Quicksort_Partition - Fatal编程技术网

Java 对整数数组使用快速排序时的分区方法

Java 对整数数组使用快速排序时的分区方法,java,sorting,quicksort,partition,Java,Sorting,Quicksort,Partition,我希望能够使用快速排序算法对整数数组进行排序,但我在使用分区方法和pivot时遇到了问题。下面是我的代码,底部有一个测试方法 public class QuickSort { public static void sort(int [] table) { quickSort(table, 0, table.length - 1); } private static void quickSort(int [] table,int first,int last) { print

我希望能够使用快速排序算法对整数数组进行排序,但我在使用分区方法和pivot时遇到了问题。下面是我的代码,底部有一个测试方法

public class QuickSort {

public static  void sort(int [] table) {
   quickSort(table, 0, table.length - 1);
}

private static void quickSort(int [] table,int first,int last) {

    printArray(table);

    if (first < last) {

        int pivIndex = partition(table, first, last);

        System.out.println("pivindex = "+pivIndex);
        quickSort(table, first, pivIndex - 1);


        quickSort(table, pivIndex + 1, last);
    }
}

private static void bubbleSort(int[] table,int first,int last)
{

 int middle = (first + last) / 2;


    if (table[first]>table[middle]) {

        swap(table,first,middle);
    }

 if (table[middle]>table[last]) {

        swap(table,middle,last);
    }

    if (table[middle]<table[first]) {

        swap(table,first,middle);
    }

  swap(table,first,middle);
}

private static int partition(int[] table,int first,int last) {

    bubbleSort(table, first, last);

    Integer pivot = table[0];

    int start = first;
    int end = last;
    while(start<end)
    {
        while((start<last)&&table[start]<=pivot)
        {start++;
        }

        while(table[end]>pivot)
        {
                end--;
        }

        if(start<end)
        {
            swap(table, start, end);

        }
    }
    printArray(table);
    swap(table, first, end);

    return end;        
}
private static  void swap(int [] table, int i, int j) {
    int temp = table[i];
    table[i] = table[j];
    table[j] = temp;
}
private static  void printArray(int [] array) {
    for (int i = 0; i < array.length; i++) {
        System.out.print(array[i]);

        if (i < (array.length + 1)) {
            System.out.print(" ");
        }
    }
    System.out.println();
}

public static void main(String[] args) {
    int [] array =  {42,37,45,70,12,19, 39, 43, 61,7,99};
    sort(array);
}

}
公共类快速排序{
公共静态无效排序(int[]表){
快速排序(表,0,表.length-1);
}
私有静态void快速排序(int[]表,int first,int last){
打印阵列(表);
如果(第一次<最后一次){
int-pivIndex=分区(表,第一个,最后一个);
System.out.println(“pivindex=“+pivindex”);
快速排序(表,第一,pivIndex-1);
快速排序(表,pivIndex+1,最后一个);
}
}
私有静态void bubbleSort(int[]表,int first,int last)
{
中间整数=(第一个+最后一个)/2;
if(表[第一]>表[中间]){
交换(表,第一,中间);
}
if(表[中间]>表[最后]){
交换(表、中间、最后);
}

如果(表[中]您遇到了什么问题?请尽可能具体地回答您的问题。当我运行程序时,数组没有按升序打印,因此它没有正确排序-我需要有关代码的帮助,以使分区和排序正常工作,因为我所做的没有纠正。您的问题之一是分区函数调用了bubbl数组上的eSort。这无法正确执行。您应该重新查看您的学习资料,并尝试了解哪些内容不起作用。StackOverflow不用于调试您的代码。