Sajib_Ahmed

Quick sort

Aug 2nd, 2019
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. void quicksort(int a[],int low,int high);
  4. int partiation(int a[],int low,int high);
  5. int main()
  6. {
  7.     int a[]={4,6,8,3,5,2,0,15};
  8.     int i,l=sizeof(a)/sizeof(a[0]);
  9.     quicksort(a,0,l-1);
  10.     for(i=0;i<l;i++)
  11.     {
  12.         cout<<a[i]<<endl;
  13.     }
  14. }
  15. void quicksort(int a[],int low,int high)
  16. {
  17.     if(low<high)
  18.     {
  19.         int pi=partiation(a,low,high);
  20.         quicksort(a,low,pi-1);
  21.         quicksort(a,pi+1,high);
  22.     }
  23. }
  24. int partiation(int a[],int low,int high)
  25. {
  26.     int pivot=a[high];
  27.     int i=low-1,j;
  28.     for(j=low;j<high;j++)
  29.     {
  30.         if(a[j]<=pivot)
  31.         {
  32.             i++;
  33.             swap(a[i],a[j]);
  34.  
  35.         }
  36.     }
  37.     swap(a[i+1],a[high]);
  38.     return (i+1);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment