-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrandomizedQuickSort.cpp
67 lines (56 loc) · 1013 Bytes
/
randomizedQuickSort.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <bits/stdc++.h>
using namespace std;
void shuffle(int *a,int s,int e){
srand(time(NULL));
int j;
for(int i = e; i > 0; i--){
//Create one random index
j = rand()%(i);
swap(a[i],a[j]);
}
return ;
}
int partition(int *a,int s,int e){
int i = s - 1;
int j = s;
int pivot = a[e];
for(;j < e;j++){
if(a[j] <= pivot){
i++;
swap(a[i],a[j]);
}
}
//Bring the pivot element to its right position
swap(a[e],a[i+1]);
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 argc, char const *argv[])
{
int n;
cout<<"Enter the number of elements of array \n";
cin >> n;
int a[n];
for(int i = 0; i < n; i++){
cin >> a[i];
}
shuffle(a,0,n-1);
cout<<"Shuffled array ";
for(int i = 0; i < n; i++){
cout << a[i] << " ";
}
cout << endl;
quickSort(a,0,n-1);
cout << "Sorted array ";
for(int i = 0; i < n; i++){
cout << a[i] << " ";
}cout<<endl;
return 0;
}