JAVA快速排序,分区方法的错误是什么? /** *按升序对数组排序 *@param arr要排序的数组 */ 公共静态void快速排序(int[]arr){ qs(arr,0,arr.length); } /** *按升序对数组的区域进行排序。 *给定区域之外的元素不变。 *要求:0

JAVA快速排序,分区方法的错误是什么? /** *按升序对数组排序 *@param arr要排序的数组 */ 公共静态void快速排序(int[]arr){ qs(arr,0,arr.length); } /** *按升序对数组的区域进行排序。 *给定区域之外的元素不变。 *要求:0,java,quicksort,Java,Quicksort,让我强调几个重要事实: /** * Sort an array in ascending order * @param arr array to sort */ public static void quickSort(int[] arr){ qs(arr, 0, arr.length); } /** * Sort a region of an array in ascending order. * Elemen

让我强调几个重要事实:

/**
     * Sort an array in ascending order
     * @param arr array to sort
     */
    public static void quickSort(int[] arr){
        qs(arr, 0, arr.length);
    }

/**
     * Sort a region of an array in ascending order.
     * Elements outside the given region are unchanged.
     * requires: 0 <= start <= end <= arr.length
     * @param arr   array to sort
     * @param start start of region (inclusive)
     * @param end   end of region (exclusive)
     */
    private static void qs(int[] arr, int start, int end){
        if (end <= start+1){    //region of length 0 or 1
            return;
        }
        int x = arr[start];
        int p = partition(arr, start+1, end, x);
            //now swap arr[start] with arr[p-1]
        arr[start] = arr[p-1];
        arr[p-1] = x;
        qs(arr, start, p-1);
        qs(arr, p, end);



    }

    /**
     * Partition a region of an array.
     * Rearranges elements in region so that small ones
     *   all have smaller indexes than the big ones.
     * Elements outside the region are unchanged.
     * requires: 0 <= start <= end <= arr.length
     * @param arr   array to partition
     * @param start start of region (inclusive)
     * @param end   end of region (exclusive)
     * @param x     pivot - "small" and "big" are <x, >=x.
     * @return      start index (inclusive) of big elements
     *              in region after partition.
     */
    private static int partition(
        int[] arr, int start, int end, int x)
    {

        int l = start -1, r = end+1;
        while (true) {
            while (l < end && arr[++l] < x) ;

            while(r> l && arr[--r] >x);// find smaller item

                if(l >= r) // if pointers cross,
                    break; // partition done
                    else  {
                        int temp;
                temp = arr[l];
                arr[l] = arr[r];
                arr[r] = temp;
            }
        }
        return l;
        }


    public static void main(String[] args) {
        int[] a = {15,8,9,6,2,8,8,5,8,4,2};
        quickSort(a);
        for (int i = 0; i < a.length; i++){
            System.out.print(" "+a[i]);
        }
    }
}


或者您也可以将更高的索引包含在内,并从
main

调用
qs(arr,0,arr.length-1)
它在哪一行抛出arrayindexoutofbounds例外?堆栈跟踪说明了什么?我认为我需要使r=end+1并更改循环,因为我保留r=end最后一个索引将保持不变。您希望
r
在要分区的数组块之后启动一个索引。这意味着,
end
是包含的,您以
r=end+1
开始,或者
end
是独占的,
r
end
开始。你可以选择。嗯,有一些更为传统的变体,在循环体中使用增量和减量,而不是循环控制;然后把循环改为while(++l * @param end end of region (exclusive) ^^^^^^^^^ * @param x pivot - "small" and "big" are <x, >=x. * @return start index (inclusive) of big elements * in region after partition. */ private static int partition( int[] arr, int start, int end, int x) { int l = start -1, r = end+1; ^^^^^^^^^^ while (true) { while (l < end && arr[++l] < x) ; ^^^^^^^^^^^^^^^^^^^^^^^ while(r> l && arr[--r] >x);// find smaller item ^^^^^^^^
while(++l < r && arr[l] < x)
while(r-- > l && arr[r] > x)