forked from EnigmaVSSUT/Hacktoberfest2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.cpp
48 lines (44 loc) · 796 Bytes
/
quicksort.cpp
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
48
#include<iostream>
using namespace std;
void swap(int *a,int *b){
int tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
int partition(int A[],int low,int high){
int pivot;
pivot=A[high];
int i=low-1;
for(int j=low;j<high;j++){
if(A[j]<=pivot){
i++;
swap(&A[i],&A[j]);
}
}
swap(&A[i+1],&A[high]);
return(i+1);
}
void quickSort(int A[],int low,int high){
if(low<high){
int pt=partition(A,low,high);
quickSort(A,low,pt-1);
quickSort(A,pt+1,high);
}
}
int main(){
int n;
cout<<"Enter the number of array elements \n";
cin>>n;
int a[n];
cout<<"Enter the array elements \n";
for(int i=0;i<n;i++){
cin>>a[i];
}
quickSort(a,0,n-1);
cout<<"The elements in sorted order are \n";
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
return 0;
}