-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRandomizedQuickSort.cpp
More file actions
58 lines (53 loc) · 1.12 KB
/
RandomizedQuickSort.cpp
File metadata and controls
58 lines (53 loc) · 1.12 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
#include <bits/stdc++.h>
using namespace std;
void shuffleArray(int *A, int s, int e){
srand(time(NULL));
for (int i=s;i<e;i++){
int j=rand()%(i+1);
swap(A[i], A[j]);
}
cout<<"\nRandomized Array : "<<endl;
for (int i=0;i<9;i++){
cout<<A[i];
}
}
int partitionArray(int * A, int lb, int ub){
int pivot=A[lb];
int i=lb;
int j=ub;
while (i<j){
while (A[i]<=pivot){
i++;
}
while (A[j]>pivot){
j--;
}
if (i<j){
swap(A[i], A[j]);
}
}
swap(A[lb], A[j]);
return j;
}
void quickSort(int * A, int lb, int ub){
if (lb<ub){
int location=partitionArray(A, lb, ub);
quickSort(A, lb, location-1);
quickSort(A, location+1, ub);
}
}
int main()
{
int A[]={7, 6, 10, 5, 9, 2, 1, 15, 7};
cout<<"Array : "<<endl;
for (int i=0;i<9;i++){
cout<<A[i];
}
cout<<"\n";
shuffleArray(A, 0, 8);
quickSort(A, 0, 8);
for (int i=0;i<9;i++){
cout<<A[i];
}
return 0;
}