Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.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_Algorithm_Sorting_Quicksort - Fatal编程技术网

java中的快速排序代码运行不正常?

java中的快速排序代码运行不正常?,java,algorithm,sorting,quicksort,Java,Algorithm,Sorting,Quicksort,我被这件事困扰了一段时间。。。数组没有正确排序,结尾也没有递减。有人能帮我解决我在这里到底犯了什么错误吗? 我试了好几个小时,但没有找到确切的错误。您可以在输出中清楚地看到end没有递减,数组的排序也不正确,因为17是最后一个元素,45是倒数第二个元素。此外,还有边界排列的错误 代码: 公共类程序{ 公共静态void main(字符串[]args){ int[]ar={4,5,6,3,10,12,45,17}; int l=0; int h=ar.length-1; 快速排序(ar、l、h);

我被这件事困扰了一段时间。。。数组没有正确排序,结尾也没有递减。有人能帮我解决我在这里到底犯了什么错误吗? 我试了好几个小时,但没有找到确切的错误。您可以在输出中清楚地看到end没有递减,数组的排序也不正确,因为17是最后一个元素,45是倒数第二个元素。此外,还有边界排列的错误

代码:

公共类程序{
公共静态void main(字符串[]args){
int[]ar={4,5,6,3,10,12,45,17};
int l=0;
int h=ar.length-1;
快速排序(ar、l、h);
}
静态无效快速排序(int[]arr、int-lb、int-ub){
intz;
if(lb
类快速排序
{ 
整数分区(整数arr[],整数低,整数高)
{ 
int pivot=arr[高];
int i=(低-1);//较小元素的索引

对于(int j=low;j“数组未正确排序,结尾未递减”-您是如何确定的?给出一个输入、预期结果和实际结果的示例。请参阅和。内部while循环(
while(a[start])欢迎使用StackOverflow。请参阅。在您发布MRE代码并准确指定问题之前,我们无法有效帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您指定的问题。
     public class Program{

    public static void main(String[] args) {
    int [] ar={4,5,6,3,10,12,45,17};
    int l=0;
    int h=ar.length-1;

    quickSort(ar,l,h);


}
    static void quickSort(int [] arr,int lb,int ub){
    int z;
    if(lb<ub){
    z=partition(arr,lb,ub);
    quickSort(arr,lb,z-1);
    quickSort(arr,z+1,ub);
    }

    for(int i=0;i<arr.length;i++){
        System.out.println(arr[i]);
    }
}


static int partition(int [] a,int lb,int ub){
    int pivot=a[lb];
    int start=lb;
    int end=ub;
   //If I put end=ub-1 here then it shows index-1. 
   //so I have not options left to do that the Error is coming eitherway.
    System.out.println("start is "+start);
    System.out.println("End is "+end);

    while(start<end){
        while(a[start]<=pivot){
            start++;
        }
        while(a[end]>pivot){
            end--;
        }
        if(start<end){
        int temp0=a[start];
        int temp1=a[end];
        a[start]=temp1;
        a[end]=temp0;
        }

    }
        int temp2=a[lb];
        int temp3=a[end];
        a[lb]=temp3;
        a[end]=temp2;
        return end;
}
}

output:
start is 0
End is 7
3
4
6
5
10
12
45
17
start is 2
End is 7
3
4
5
6
10
12
45
17
start is 4
End is 7
3
4
5
6
10
12
45
17
start is 5
End is 7
3
4
5
6
10
12
45
17
start is 6
End is 7


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 8
    at Program.partition(Program.java:35)
    at Program.quickSort(Program.java:16)
    at Program.quickSort(Program.java:18)
    at Program.quickSort(Program.java:18)
    at Program.quickSort(Program.java:18)
    at Program.quickSort(Program.java:18)
    at Program.main(Program.java:9)
    class QuickSort 
{ 
    int partition(int arr[], int low, int high) 
    { 
        int pivot = arr[high]; 
        int i = (low-1); // index of smaller element 
        for (int j=low; j<high; j++) 
        { 
            // If current element is smaller than the pivot 
            if (arr[j] < pivot) 
            { 
                i++; 

                // swap arr[i] and arr[j] 
                int temp = arr[i]; 
                arr[i] = arr[j]; 
                arr[j] = temp; 
            } 
        } 

        // swap arr[i+1] and arr[high] (or pivot) 
        int temp = arr[i+1]; 
        arr[i+1] = arr[high]; 
        arr[high] = temp; 

        return i+1; 
    } 

    void sort(int arr[], int low, int high) 
    { 
        if (low < high) 
        { 
            /* pi is partitioning index, arr[pi] is 
            now at right place */
            int pi = partition(arr, low, high); 

            // Recursively sort elements before 
            // partition and after partition 
            sort(arr, low, pi-1); 
            sort(arr, pi+1, high); 
        } 
    } 

    /* A utility function to print array of size n */
    static void printArray(int arr[]) 
    { 
        int n = arr.length; 
        for (int i=0; i<n; ++i) 
            System.out.print(arr[i]+" "); 
        System.out.println(); 
    } 

    // Driver program 
    public static void main(String args[]) 
    { 
        int arr[] = {4,5,6,3,10,12,45,17}; 
        int n = arr.length; 

        QuickSort ob = new QuickSort(); 
        ob.sort(arr, 0, n-1); 

        System.out.println("sorted array"); 
        printArray(arr); 
    } 
}