排序算法
bool cmp(Student a,Student b){
if(a.score != b.score)return a.score>b.score;
else return strcmp(a.name,b.name)<0;//注意这里strcmp函数
}基础排序:冒泡、选择、插入
计数排序

快速排序
第k大问题
Last updated
bool cmp(Student a,Student b){
if(a.score != b.score)return a.score>b.score;
else return strcmp(a.name,b.name)<0;//注意这里strcmp函数
}
Last updated
//[lo,hi]勤于扩展、懒于交换,全部元素相同时是最坏情况
int partition(vector<int>& vec,int lo,int hi){
swap(vec[lo],vec[rand()%(hi-lo+1)+lo]);
int tmp=vec[lo];
while(lo<hi){
while(lo<hi&&vec[hi]>=tmp)hi--;
vec[lo]=vec[hi];
while(lo<hi&&vec[lo]<=tmp)lo++;
vec[hi]=vec[lo];
}
vec[lo]=tmp;
return lo;
}
int randSelect(vector<int>& vec,int lo,int hi,int k){
if(lo==hi)return vec[lo];
int p=partition(vec,lo,hi);
//_for(i,lo,p)if(vec[i]>vec[p])printf("%derror\n",i);
//_for(i,p,hi+1)if(vec[i]<vec[p])printf("%derror\n",i);
int m=p-lo+1;
if(k==m)return vec[p];
if(k>m)
return randSelect(vec,p+1,hi,k-m);
else
return randSelect(vec,lo,p-1,k);
}