-
Notifications
You must be signed in to change notification settings - Fork 1
/
quickSort.h
185 lines (147 loc) · 3.23 KB
/
quickSort.h
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#pragma once
#include <iostream>
#include <chrono>
#include <stdlib.h>
#include <time.h>
using std::cin;
using std::cout;
using std::endl;
///
///AUXILIARY FUNCTIONS
///
/*
void printArray(int A[], int size) {
for (int i = 0; i < size - 1; i++) {
cout << A[i] << ", ";
}
cout << A[size - 1] << endl;
}
*/
void swap(int A[], int l, int r) {
int temp = A[l];
A[l] = A[r];
A[r] = temp;
}
int median(int first, int mid, int last) {
int median = last;
int min = last;
int max = last;
if (first < mid) {
min = first;
max = mid;
}
else {
min = mid;
max = first;
}
if (median > max) {
int temp = median;
median = max;
max = temp;
}
if (median < min) {
int temp = median;
median = min;
min = temp;
}
return median;
}
int getRand(int p, int r) {
int size = r - p + 1;
int random = rand() % size;
return random + p;
}
int generateRand() {
int random = rand() % 200;
random = random % rand();
return random;
}
///
///PIVOT IS ALWAYS A[R]
///
int partition(int A[], int p, int r) {
int x = A[r];
int i = p - 1;
for (int j = p; j < r; j++) {
if (A[j] <= x) {
++i;
swap(A, i, j);
}
}
swap(A, i + 1, r);
return i + 1;
}
int partitionFirst(int A[], int p, int r) {
swap(A, p, r);
return partition(A, p, r);
}
int partitionRandom(int A[], int p, int r) {
int random = getRand(p, r);
swap(A, random, r);
return partition(A, p, r);
}
int partitionMedian(int A[], int p, int r) {
int mid = p + (r - p) / 2;
int med = median(p, mid, r);
swap(A, med, r);
return partition(A, p, r);
}
void quick_sort(int A[], int p, int r, int input) {
if (p < r) {
int q;
if (input == 1) {
q = partitionFirst(A, p, r);
}
else if (input == 2) {
q = partitionRandom(A, p, r);
}
else if (input == 3) {
q = partitionMedian(A, p, r);
}
else {
return;
}
quick_sort(A, p, q - 1, input);
quick_sort(A, q + 1, r, input);
}
}
/*
int main() {
using namespace std::chrono;
steady_clock::time_point t1, t2;
duration<double> time_span_best, time_span_worst, time_span_average;
int input;
int best[1000]; //unsorted distinct elements
int worst[1000]; //sorted elements
int average[1000]; //unsorted with possible duplicate elements
for (int i = 0; i < 1000; i++) {
average[i] = generateRand();
best[i] = i;
worst[i] = i;
}
for (int i = 0; i < 1000; i++) {
int random = getRand(i, 1000);
swap(best, i, random);
}
quick_sort(worst, 0, 999, 1);
cout << "Enter an option for pivot:\n";
cout << "1) First element.\n2) Random element.\n3) Median of first, middle, and last element.\n";
cin >> input;
t1 = steady_clock::now();
quick_sort(best, 0, 999, input);
t2 = steady_clock::now();
time_span_best = duration_cast<duration<double>>(t2 - t1);
t1 = steady_clock::now();
quick_sort(worst, 0, 999, input);
t2 = steady_clock::now();
time_span_worst = duration_cast<duration<double>>(t2 - t1);
t1 = steady_clock::now();
quick_sort(average, 0, 999, input);
t2 = steady_clock::now();
time_span_average = duration_cast<duration<double>>(t2 - t1);
cout << "Best-case Input: " << time_span_best.count() << " seconds.\n";
cout << "Worst-case Input: " << time_span_worst.count() << " seconds.\n";
cout << "Average-case Input: " << time_span_average.count() << " seconds.\n";
return 0;
}
*/