You are on page 1of 2

#include<iostream> using namespace std; class sort { public: void quicksort( int *a, int low, int high

); void swap(int *a,int left,int right); int partition(int *a, int low, int high ); }; void sort::quicksort(int *a, int low, int high ) { int pivot; /* Termination condition! */ if ( high > low ) { pivot = partition( a, low, high ); quicksort( a, low, pivot-1 ); quicksort( a, pivot+1, high ); } } void sort::swap(int *a,int left,int right) { int temp; temp=a[left]; a[left]=a[right]; a[right]=temp; } int sort::partition(int *a, int low, int high ) { int left, right; int pivot_item; pivot_item = a[low]; int pivot = left = low; right = high; while ( left < right ) { /* Move left while item < pivot */ while( a[left] <= pivot_item ) left++; /* Move right while item > pivot */ while( a[right] > pivot_item ) right--; if ( left < right ) swap(a,left,right); } /* right is final position for the pivot */ a[low] = a[right]; a[right] = pivot_item; cout<<right<<endl; return right; }

int main() { sort ob; int a[10]={12,3,3,6,71,11,23,44,80,66}; ob.quicksort(a,0,9);

int x; for(int i=0;i<10;i++) cout<<"a["<<i<<"] "<<a[i]<<endl; cin>>x; return 0; }

You might also like