1,快排

int partion(int *x,int low,int high){

   int temp=x[low];
   int i=low;
   int j=high+1;

   while(true){
     while(x[++i]<temp);
     while(x[--j]>temp);
     if(i>=j) break;
     swap(x[i],x[j]);
   }
   x[low]=x[j];
   x[j]=temp;
   return j;
}
int quickpass(int *x,int low,int high){
    if(low<high){
        int p=partion(x,low,high);
        quickpass(x,low,p-1);
        quickpass(x,p+1,high);
    }
}

2,根据快排中的partion函数可以有很多的变形

求数组的第k小的元素

//求第k小的元素
int mink(int *m,int low,int high,int k){

    if(low==high) return m[low];
    int f=partion(m,low,high);
    //左边的元素的个数
int lnum=f-low+1; if(lnum>=k) return mink(m,low,f,k); else return mink(m,f+1,high,k-lnum); }

 

相关文章:

  • 2021-11-19
  • 2021-07-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-29
  • 2022-12-23
  • 2021-11-24
猜你喜欢
  • 2021-11-11
  • 2022-12-23
  • 2021-11-11
  • 2022-02-27
  • 2022-12-23
  • 2021-11-19
  • 2022-12-23
相关资源
相似解决方案