-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_quickSort.cpp
97 lines (81 loc) · 1.85 KB
/
03_quickSort.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
using namespace std;
void input(int arr[], int n)
{
cout << "Enter " << n << " numbers: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
}
void print(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
// helper function for quicksort
pair<int, int> partition(int arr[], int begin, int end)
{
// randomized sorting
srand(time(NULL));
int random = begin + rand() % (end - begin);
swap(arr[random], arr[end]);
// simple algo
int pivot = arr[end];
int cnt = 0;
int i = begin - 1;
for (int j = begin; j < end; j++)
{
cnt++;
if (arr[j] <= pivot)
swap(arr[++i], arr[j]);
}
swap(arr[++i], arr[end]);
return {i, cnt};
}
// main function
int quickSort(int arr[], int begin, int end)
{
if (begin >= end)
return 0;
pair<int, int> data = partition(arr, begin, end);
int pivot = data.first;
int cnt = data.second;
cnt += quickSort(arr, begin, pivot - 1);
cnt += quickSort(arr, pivot + 1, end);
return cnt;
}
// caller function
int quickSort(int arr[], int n)
{
return quickSort(arr, 0, n - 1);
}
int partitionYT(int arr[], int low, int high)
{
int pivot = low, i = low, j = high;
while (i < j)
{
while (i <= high && arr[i] <= arr[pivot])
i++;
while (j >= low && arr[j] > arr[pivot])
j--;
if (i < j)
swap(arr[i], arr[j]);
}
swap(arr[j], arr[pivot]);
return j;
}
int main()
{
cout << "Enter n: ";
int n;
cin >> n;
int arr[n];
input(arr, n);
// *******************************
int num_comp = quickSort(arr, n);
// *******************************
cout << "\nArray after sorting:\n";
print(arr, n);
cout << "\nNumber of comparisons: " << num_comp;
cout << endl;
}