当m=k时,返回第m个数;
当m>k时,在左侧查找;
当m<k时,在右侧查找.
int onequick(int A[], int low, int high) {
int temp = A[low];
while (low < high) {
while (low < high && A[high] >= temp)
high--;
A[low] = A[high];
while (low < high && A[low] < temp)
low++;
A[high] = A[low];
}
A[low] = temp;
return low;
}
int quicksort(int A[], int low, int high, int k) {
int m;
if (low <= high) {
m = onequick(A, low, high);
if (m == k - 1) return A[m];
else if (m < k - 1) return quicksort(A, m + 1, high, k);
else return quicksort(A, low, m - 1, k);
}
else
return -1;
}
int main()
{
int A[] = { 15, 6, 20, 17, 13, 45, 30, 25 };
printf("%d\n", quicksort(A, 0, 7, 2));
return 0;
}