-
-
Save amankharwal/f50a17ec0b6104c1a9e50cb4f5ab0b4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
void swap(int arr[], int i, int j){ | |
int temp = arr[i]; | |
arr[i] = arr[j]; | |
arr[j] = temp; | |
} | |
int partition(int arr[], int l, int r){ | |
int pivot = arr[r]; | |
int i = l - 1; | |
for(int j = l; j < r; j++){ | |
if(arr[j] < pivot){ | |
i++ | |
swap(arr, i, j); | |
} | |
} | |
swap(arr, i + 1, r); | |
return i+1; | |
} | |
void quicksort(int arr[], int l, int r){ | |
if(l < r){ | |
int pi = partition(arr, l, r); | |
quicksort(arr, l, pi-1); | |
quicksort(arr, pi+1, r); | |
} | |
} | |
int main(){ | |
int arr[5]={5, 4, 3, 2, 1}; | |
quicksort(arr, 0, 4); | |
for(int i=0; i<5; i++){ | |
cout<<arr[i]<<" "; | |
}cout<<endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment