forked from ankur-b/Sorting
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathquicksort.cpp
More file actions
47 lines (45 loc) · 713 Bytes
/
quicksort.cpp
File metadata and controls
47 lines (45 loc) · 713 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
#define n 8
using namespace std;
int partition(int a[],int first,int last){
int pivot=a[first],i=first,j=last+1;
do{
do{
++i;
}while(a[i]<pivot && i<=last);
do{
--j;
}while(pivot<a[j]);
if(i<j){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);
a[first]=a[j];
a[j]=pivot;
return first;
}
void quicksort(int a[],int p, int q){
if(p<q){
int x=partition(a,p,q);
quicksort(a,p,x-1);
quicksort(a,x+1,q);
}
}
void print(int a[]){
for(int i=0;i<n;i++){
cout<<a[i]<<endl;
}
}
int main(){
cout<<"enter "<<n<<" elements:\n";
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<"sorted array: \n";
quicksort(a,0,n-1);
print(a);
return 0;
}