Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created Dec 25, 2020
Embed
What would you like to do?
#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