#include<stdio.h>
int Kth_elem(int a[], int low, int high, int k)
{
int pivot = a[low];
int low_temp = low;
int high_temp = high;
while(low<high)
{
while(low<high && a[high]>=a[low])
--high;
a[low]=a[high];
while(low<high && a[low]<=a[high])
++low;
a[high]=a[low];
}
a[low]=pivot;
if(low==k)
return a[low];
else if (low>k)
return Kth_elem(a,low_temp,low-1,k);
else
return Kth_elem(a,low+1,high_temp,k);
}
int main()
{
int data[10]={1,31,4,52,6,4,77,41,62,94};
printf("%d ",Kth_elem(data,0,9,1));
}
寻找第K大的数
最新推荐文章于 2022-01-12 14:56:48 发布