Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created December 25, 2020 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amankharwal/f50a17ec0b6104c1a9e50cb4f5ab0b4a to your computer and use it in GitHub Desktop.
Save amankharwal/f50a17ec0b6104c1a9e50cb4f5ab0b4a to your computer and use it in GitHub Desktop.
#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