找第k小的元素(基于快速排序的思想)
#include<stdio.h>
#include<stdlib.h>
int Select_Nok(int a[], int low, int high,int k) {
int pivot = a[low];
int low_temp = low; //在后续递归调用中需要用到最初调用时的low和high,而在之后的调用中会修改low和high,所以暂时保存下来
int high_temp = high;
while (low < high) {
while (low < high && a[high] >= pivot) high--;
a[low] = a[high];
while (low < high && a[low <= pivot]) low++;
a[high] = a[low];
}
a[low] = pivot;
return low;
if (low == k)
return a[low];
else if (low > k)
return Select_Nok(a, low_temp, low - 1, k);
else
return Select_Nok(a, low + 1, high_temp, k);
}