forked from MukulCode/CodingClubIndia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomizedQuickSort.cpp
48 lines (48 loc) · 1.1 KB
/
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
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
void shuffle(int arr[]) {
int n = sizeof(arr) / sizeof(int);
srand(time(NULL));
int j;
for (int i = n - 1; i > 0; i--) {
j = rand() % (i + 1);
swap(arr[i], arr[j]);
}
}
bool compare(int a, int b) {
return a > b;
}
int partition(int arr[], int s, int e, bool (&compare)(int a, int b)) {
int i = s - 1;
int j = s, pivot = arr[e];
for (; j < e; j++) {
if (compare(arr[j], pivot)) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[e]);
return i + 1;
}
void quickSort(int arr[], int s, int e, bool (&compare)(int a, int b)) {
if (s >= e)
return;
int p = partition(arr, s, e, compare);
quickSort(arr, s, p - 1, compare);
quickSort(arr, p + 1, e, compare);
}
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
shuffle(arr);
quickSort(arr, 0, n - 1, compare);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}