-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathQuicksort.h
55 lines (47 loc) · 1.13 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
//
// Quicksort.h
// Quicksort
//
// Created by Xiaohang Su on 2/21/17.
// Copyright © 2017 Xiaohang Su. All rights reserved.
//
#ifndef Quicksort_h
#define Quicksort_h
#include <vector>
#include <algorithm>
template <typename T>
int partition(std::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;
std::swap(A[i], A[j]);
}
}
std::swap(A[i + 1], A[r]);
return i + 1;
}
template <typename T>
void quicksort(std::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 randomized_partition(std::vector<T>& A, int p, int r) {
int i = (rand() % (r - p)) + p;
std::swap(A[r], A[i]);
return partition(A, p, r);
}
template <typename T>
void randomized_quicksort(std::vector<T>& A, int p, int r) {
if (p < r) {
int q = randomized_partition(A, p, r);
randomized_quicksort(A, p, q - 1);
randomized_quicksort(A, q + 1, r);
}
}
#endif /* Quicksort_h */