这是3路快速排序...
private void quicksort3(int[] input, int low, int high)
{
int i = low - 1, j = high, left = low - 1, right = high, pivot = input[high], k;
if (high <= low) return;
while (true)
{
while (input[++i] < pivot) ;
while (pivot < input[--j])
if (j == low) break;
if (i >= j) break;
swap(input, i, j);
if (input[i] == pivot)
{
left++;
swap(input, left, i);
}
if (pivot == input[j]) {
right--;
swap(input, j, right);
}
}
swap(input, i, high); j = i - 1; i++;
for (k = low; k < left; k++, j--)
swap(input, k, j);
for (k = high - 1; k > right; k--, i++)
swap(input, i, k);
quicksort3(input, low, j);
quicksort3(input, i, high);
}
对于输入9 5 3
,我得到异常错误Index was outside the bounds of the array.
,此处发生异常,pivot = input[high]
其中 high 的值为 -1(所以我明白为什么我有错误)但是我该如何解决呢?
我这样调用函数quicksort3(arr,0,arr.Length-1);