-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquickSort.cpp
More file actions
85 lines (70 loc) · 1.36 KB
/
quickSort.cpp
File metadata and controls
85 lines (70 loc) · 1.36 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<bits/stdc++.h>
#include<cmath>
using namespace std;
void exchange(int *a, int *b){
int tmp = *a;
*a = *b;
*b = tmp;
}
int quick_partitionRight(vector<int> &a, int l , int r){
int pivot = a[r];
int i = l- 1;
for(int j = l; j < r; j++){
if(a[j] <= pivot){
i++;
exchange(&a[i], &a[j]);
}
}
exchange(&a[i+1], &a[r]);
return i +1;
}
int randomized_partition(vector<int> &a , int l , int r){
int i = (rand() % r) + l;
exchange(&a[i],&a[r]);
return quick_partitionRight(a,l,r);
}
int quick_partitionLeft(vector<int> &a, int l , int r){
int pivot = a[l];
int i = l +1;
for(int j = l+1 ; j <= r; j++){
if(a[j] <= pivot){
if(i != l +1){
exchange(&a[i],&a[j]);
}
i++;
}
}
exchange(&a[i-1],&a[l]);
return i-1;
}
void quickSort(vector<int> &a, int l, int r, char choice){
if(l < r){
int q;
if(choice == 'L'){ // left pivot
q = quick_partitionLeft(a,l,r);
}
if(choice == 'H'){ // right pivot
q = quick_partitionRight(a,l,r);
}
if(choice == 'R'){ // randomized pivot
q = randomized_partition(a,l,r);
}
quickSort(a,l,q-1,choice);
quickSort(a,q+1,r,choice);
}
}
int main(){
int n;
cin>>n;
cout<<"Enter pivot choice:\n";
char choice;
cin>>choice;
vector<int> a(n);
for(int i = 0; i < n; i++){
cin>>a[i];
}
quickSort(a,0, n-1,choice);
for(int i = 0; i < n; i++){
cout<<a[i]<<" ";
}
}