-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path37.cpp
More file actions
51 lines (48 loc) · 951 Bytes
/
Copy path37.cpp
File metadata and controls
51 lines (48 loc) · 951 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
48
49
50
51
// Quick sort - lomuto
#include<iostream>
using namespace std;
int partition(int a[], int lb, int ub){
int i,j,pivot;
pivot = a[ub];
i = lb - 1;
for(j=lb;j<ub;j++){
if(a[j]<=pivot){
i++;
swap(a[i],a[j]);
}
}
swap(a[i+1],a[ub]);
return i+1;
}
void quicksort(int a[], int lb, int ub){
int p;
if(lb<ub){
p = partition(a,lb,ub);
quicksort(a,lb,p-1);
quicksort(a,p+1,ub);
}
}
int main(){
int a[20],i,n;
cout<<"Enter the no. of elements: ";
cin>>n;
cout<<"Enter elements:\n";
for(i=0;i<n;i++){
cout<<"Element "<<i+1<<": ";
cin>>a[i];
}
cout<<"\nUnsorted Array:\n";
cout<<"[";
for(i=0;i<n;i++){
cout<<" "<<a[i];
}
cout<<"]\n";
quicksort(a,0,n-1);
cout<<"\nSorted Array:\n";
cout<<"[";
for(i=0;i<n;i++){
cout<<" "<<a[i];
}
cout<<"]\n";
return 0;
}