spider68

quick sort

May 18th, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.39 KB | None | 0 0
  1.  
  2. int partition (int arr[], int l, int h)
  3. {
  4.    int p=arr[l];
  5.    int i=l+1,j=h;
  6.    while(i<=j)
  7.    {
  8.        while(arr[i]<=p)i++;
  9.        while(arr[j]>p)j--;
  10.        if(i<=j)swap(arr[i],arr[j]);
  11.    }
  12.    swap(arr[l],arr[j]);
  13.    return j;
  14. }
  15. void quicksort(int arr[],int l,int h)
  16. {
  17.   if(l<h)
  18.   {
  19.     int pi=partition(arr,l,h);
  20.     quicksort(arr,l,pi-1);
  21.     quicksort(arr,pi+1,h);
  22.   }
  23. }
Add Comment
Please, Sign In to add comment