📄 排序后第k位置数.cpp
字号:
double select(int low,int high,int k)
{
if(low==high) return a[low];
int r = low+rand()%(high-low+1);
double t=a[r];a[r]=a[low];a[low]=t;
int t1,t2;
t1=low;t2=high;
while(t1<t2){
while(!(a[t2]+eps<t)&&t2>t1) t2--;
a[t1]=a[t2];
while(!(a[t1]>t+eps)&&t1<t2) t1++;
a[t2]=a[t1];
}
a[t1]=t;
if(t1==k) return a[t1];
if(t1<k) return select(t1+1,high,k);
if(t1>k) return select(low,t1-1,k);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -