-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path7.1.h
50 lines (42 loc) · 839 Bytes
/
7.1.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
#ifndef ch7_1_h
#define ch7_1_h
#include <vector>
#include "../print.h"
using std::swap;
using std::vector;
// [Page 171]
// QUICKSORT(A, p, r)
// - Average time: O(n lg n)
// - Worst time: O(n^2)
//
// PARTITION(A, p, r)
// - Time: O(n)
namespace CLRS {
namespace CH7 {
template <typename T>
int partition(vector<T>& A, int p, int r);
template <typename T>
void quicksort(vector<T>& A, int p, int r) {
if (p < r) {
int q = partition(A, p, r);
quicksort(A, p, q - 1);
quicksort(A, q + 1, r);
}
}
template <typename T>
int partition(vector<T>& A, int p, int r) {
T x = A[r];
int i = p - 1;
for (int j = p; j <= r - 1; j++) {
if (A[j] <= x) {
i = i + 1;
swap(A[i], A[j]);
}
print(A);
}
swap(A[i + 1], A[r]);
return i + 1;
}
} // namespace CH7
} // namespace CLRS
#endif