-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort.cpp
More file actions
65 lines (48 loc) · 1.38 KB
/
Copy pathquicksort.cpp
File metadata and controls
65 lines (48 loc) · 1.38 KB
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
#include <algorithm>
#include <array>
#include <iostream>
#include <random>
#include <catch2/catch.hpp>
//as Container you can use std::array, std::vector or std::deque
template <typename Container>
uint partition(Container &array, uint low, uint high) {
auto pivot = high;
auto firstHigh = low;
for (size_t i = low; i < array.size(); ++i) {
if (array[i] < array[pivot]) {
std::swap(array[i], array[firstHigh]);
++firstHigh;
}
}
std::swap(array[pivot], array[firstHigh]);
return firstHigh;
}
template <typename Container>
void quicksort(Container &array, uint low, uint high) {
if (low < high) {
auto part = partition(array, low, high);
quicksort(array, low, part - 1);
quicksort(array, part + 1, high);
}
}
template <typename Container> void quicksort(Container &array) {
quicksort(array, 0, array.size() - 1);
}
std::random_device seed;
std::mt19937 engine(seed());
std::uniform_int_distribution<> generator(1, 30);
TEST_CASE() {
constexpr uint SizeArray{20};
std::array<int, SizeArray> array;
std::generate(array.begin(), array.end(), []() { return generator(engine); });
auto printArray = [](const auto &array) {
for (const auto &var : array) {
std::cout << var << " ";
}
std::cout << std::endl;
};
printArray(array);
quicksort(array);
printArray(array);
CHECK(std::is_sorted(array.begin(), array.end()));
}