Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions quicksort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Quick Sort
Avg Performance O(nlogn)
Worst Performance O(n^2)
Space Compexity O(logn)
Not Stable
*/

#include<iostream>
using namespace std;
int partition(int a[],int l,int h)
{
int i=l,j=h+1,pivot;
pivot=a[l];
while(i<=j)
{
do{
i++;
}while(a[i]<=pivot);
do{
j--;
}while(a[j]>pivot);
if(i<j){
swap(a[i],a[j]);
}
}
swap(a[l],a[j]);

return j;
}
void quicksort(int a[],int l,int h)
{ if(l<h){
int t=partition(a,l,h);
quicksort(a,l,t-1);
quicksort(a,t+1,h);
}
}
void swap(int &x,int &y)
{
int t;t=x;x=y;y=t;
}
int main()
{
int i,n;
cout<<"Enter the number of elements:- ";
cin>>n;
int a[n];
cout<<"Enter the Array elements:- ";
for(i=0;i<n;i++)
cin>>a[i];
quicksort(a,0,n-1);
cout<<"After sorting:- ";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}