N个数中第k大的元素

来源:互联网 发布:轩辕剑之天之痕mac 编辑:程序博客网 时间:2024/04/26 16:28
// 3 implement the function : int part(int *a,int low,int high), and implement the function to find the k-st num in a arraya[low] as the pivot elementint part(int *a,int low,int high){int temp = a[low];int i = low ;for(int j=low+1;j<=high;j++){if(a[j] <= temp){i++;exchange(a[i],a[j]);}}exchange(a[i],a[low]);return i;}int find_k_st(int *a,int start,int end){if(start < end){int mid = part(a,start,end);if(mid == k-1)return a[mid];else{if(k-1 < mid)find_k_st(a,start,mid-1);elsefind_k_st(a,mid+1,end);}}}

原创粉丝点击