#include <stdio.h>
#include <conio.h>
int partition(int *R,int low,int high) //快速划分
{
int key;
key=R[low];
while(low<high)
{
while(low<high&&R[high]>key) high--;
if(low<high)
{
R[low]=R[high];
low++;
}
while(low<high&&R[low]<key) low++;
if(low<high)
{
R[high]=R[low];
high--;
}
R[low]=key;
}
return low;
}
int SELECT(int *A,int p,int r,int i) //选择
{
int k;
int q;
if(p==r)
return A[p];
q=partition(A,p,r);
k=q-p+1;
if(k==i)
return A[q];
else if(i<k)
return SELECT(A,p,q-1,i);
else
return SELECT(A,q+1,r,i-k);
}
int main()
{
int i;
int key;
int a[20]={21,43,44,2,8,9,3,12,35,7,11,32,33,99,55,13,26,34,15,24};
printf("input the num of the order statistic:");
scanf("%d",&i);
key=SELECT(a,0,19,i);
printf("the ith order statistic is %d",key);
getch();
return 0;
}
线性快速划分寻找第一个顺序统计量
最新推荐文章于 2021-12-02 18:15:36 发布