-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomized_quick_sort.cpp
More file actions
74 lines (59 loc) · 1.18 KB
/
randomized_quick_sort.cpp
File metadata and controls
74 lines (59 loc) · 1.18 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
///Implement Heap Sort(The program should report the number of comparisons)
/// MADE BY CHETAN KHANNA
#include<bits/stdc++.h>
#include<ctime>
#include<cstdlib>
using namespace std;
int c=0;
void shuffle(int *a,int s,int e){
srand(time(NULL));
int i,j, temp;
for(int i=e;i>0;i--){
///Create one random index
j = rand()%(i+1);
swap(a[i],a[j]);
}
}
int partition(int *a,int s,int e){
int i=s-1;
int j = s;
int pivot = a[e];
for( ;j<e;j++){
c++;
if(a[j]<=pivot){
i++;
swap(a[i],a[j]);
}
}
///Bring the pivot element to its sorted position
swap(a[i+1],a[e]);
return i+1;
}
void quickSort(int *a,int s,int e){
if(s>=e){
return;
}
int p = partition(a,s,e);
quickSort(a,s,p-1);
quickSort(a,p+1,e);
}
int main(){
int a[20],n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
shuffle(a,0,n-1);
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
quickSort(a,0,n-1);
cout<<"\n\nSORTED ARRAY : ";
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<"\n\nCOMPARISONS = "<<c;
return 0;
}