Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class quick{
- int arr[9] = {1,2,5,10,61,77,45,95,89};
- public:
- void quicksort(int p, int q){
- int pivot;
- if(p < q){
- pivot = partition(p, q);
- quicksort(p, pivot-1);
- quicksort(pivot+1, q);
- }
- }
- void swap(int i, int j){
- cout << arr[i] << " " << arr[j] << endl;
- int temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- cout << arr[i] << " " << arr[j] << endl;
- }
- int partition(int start, int last){
- int low = start;
- int high = last;
- float pivot = arr[low];
- do{
- do{
- low ++;
- }while(arr[low] <= pivot);
- do{
- high --;
- }while(arr[high] > pivot);
- if(low < high){
- swap(low, high);
- }
- }while(low < high);
- swap(start, high);
- return high;
- }
- void display(int n){
- cout << "Sorted:";
- for(int i = 0; i < n; i++){
- cout << " " << arr[i];
- }
- }
- };
- int main(){
- quick q;
- q.quicksort(0, 9);
- q.display(9);
- // q.quicksort(0,8);
- // q.swap(2,4);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement