Advertisement
hadiuzzaman65

quick sort

Apr 12th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int partition(int ar[],int low,int high)
  5. {
  6.     int pivot=ar[low];
  7.     int i=low;
  8.     int j=high;
  9.  
  10.     while(i<j)
  11.     {
  12.         do
  13.         {
  14.             i++;
  15.         }
  16.         while(ar[i]<=pivot);
  17.         do
  18.         {
  19.             j--;
  20.         }
  21.         while(ar[j]>pivot);
  22.         if(i<j)
  23.         {
  24.             int temp=ar[i];
  25.             ar[i]=ar[j];
  26.             ar[j]=temp;
  27.         }
  28.  
  29.  
  30.     }
  31.  
  32.     int temp=ar[low];
  33.     ar[low]=ar[j];
  34.     ar[j]=temp;
  35.     return j;
  36.  
  37. }
  38. void quick_sort(int ar[7],int low,int high)
  39. {
  40.     if(low<high)
  41.     {
  42.         int j=partition(ar,low,high);
  43.         quick_sort(ar,low,j);
  44.         quick_sort(ar,j+1,high);
  45.     }
  46.  
  47. }
  48. int main()
  49. {
  50.     int ar[]= {10,5,9,16,2,8,19};
  51.  
  52.     quick_sort(ar,0,6);
  53.     for(int i=0; i<7; i++)
  54.         cout<< ar[i]<<" ";
  55.  
  56.  
  57.     cout<<endl;
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement