Quicksort (Implementation)

#include <bits/stdc++.h>
using namespace std;

int partion(int a[],int s,int e){
int i=s-1;
int j=s;
int pivot=a[e];
for( ;j<e;j++){
if(a[j]<=pivot){
i++;
swap(a[i],a[j]);
}
j++;
}

swap(a[i+1],a[e]);

return (i+1);

}

void quick(int a[],int s,int e){
//base
if(s>=e){
return;
}
//rec
int p=partion(a,s,e);
quick(a,s,p-1);
quick(a,p+1,e);

}
int main() {
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
quick(a,0,n-1);
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
return 0;
}

@adityasharma07669 what’ your doubt and please share the code’s ide link for it.

https://ide.geeksforgeeks.org/0RsbmdYJbi

Code give wrong output when i implemented Quicksort.

@adityasharma07669 don’t do j++ at line number 13, its already being done by for-loop. Just remove it.

Also please remember to mark this doubt as resolved after solving it.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

i got my doubt cleared and it was amazing exprience and i had subbmit my feedback.

1 Like