Sorting

int partition(int l,int r,vector<int>&v){int pivot=(l+r)/2;while(l<=r){    while(v[l]<v[pivot])    l++;    while(v[r]>v[pivot])     r--;    if(l<=r)    {       swap(v[l],v[r]);       l++;        r--;     }}return l;}void quicksort(int l,int r,vector<int>&v){   int idx=partition(l,r,v);   if(l<idx-1)     quicksort(l,idx-1,v);   if(idx<r)     quicksort(idx,r,v);}void quick(){   vector<int>v;   v.push_back(10);   v.push_back(20);   v.push_back(9);   v.push_back(3);   v.push_back(4);   quicksort(0,v.size()-1,v);   for(int i=0;i<v.size();i++)    cout<<v[i]<<" ";   cout<<endl;}
  • How do you make the partition algorithm more efficient
  • Applications ? (kth largest element, getting k top elements etc. )

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store