-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquicksort
139 lines (118 loc) · 6.7 KB
/
quicksort
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
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_ALGORITHM_QUICKSORT_HPP
#define GTL_ALGORITHM_QUICKSORT_HPP
// Summary: Recursive quicksort algorithm, both full and partial versions.
namespace gtl {
/// @brief The quicksort sorting algorithm to sort arrays of values using a comparitor function.
class quicksort final {
public:
/// @brief The default comparitor type, takes two values and returns true or false.
/// @tparam type The type of the values to compare.
template<typename type>
using default_comparitor_type = bool(*)(const type& lhs, const type& rhs);
/// @brief A comparitor that will sort values into ascending order.
// /// @tparam type The type of the values to compare.
template<typename type>
constexpr static const default_comparitor_type<type> comparitor_ascending = [](const type& lhs, const type& rhs){ return lhs < rhs; };
/// @brief A comparitor that will sort values into descending order.
// /// @tparam type The type of the values to compare.
template<typename type>
constexpr static const default_comparitor_type<type> comparitor_descending = [](const type& lhs, const type& rhs){ return lhs > rhs; };
private:
/// @brief Deleted constructor.
quicksort() = delete;
private:
/// @brief Swap two values in place.
/// @param lhs The value to replace with rhs
/// @param rhs The value to replace with lhs
template <typename type>
constexpr static void swap(type& lhs, type& rhs) {
type lhs_copy = lhs;
lhs = rhs;
rhs = lhs_copy;
}
public:
/// @brief Sort function that will sort an array of values using a comparitor, using the recursive form of the quicksort algorithm.
/// @tparam type The type of the values to sort.
/// @tparam comparitor_type The type of the comparitor function used to compare pairs of values.
/// @param values The array of values to sort in place.
/// @param size The size of the array of values.
/// @param comparitor The comparitor function used to determine the order.
template<typename type, typename comparitor_type = default_comparitor_type<type>>
static void sort(type* values, const unsigned long long int size, const comparitor_type comparitor = quicksort::comparitor_ascending<type>) {
// Can only sort if there are more than two values.
if (size < 2) {
return;
}
// Always compare against the current value, left is therefore the next value and right the far end.
unsigned long long int index_left = 1;
unsigned long long int index_right = size;
// Move along the indexes swapping as needed until the left and right indexes have met.
while (index_left < index_right) {
// If the comparitor returns true advance left, otherwise swap and advance right.
if (comparitor(values[index_left], values[0])) {
++index_left;
}
else {
--index_right;
quicksort::swap(values[index_left], values[index_right]);
}
}
// Once we've finished looping over the indexes swap the current value into the center by swapping with the value at left.
--index_left;
quicksort::swap(values[index_left], values[0]);
// Now recurse and sort either side of the center.
quicksort::sort(&values[0], index_left + 1, comparitor);
quicksort::sort(&values[index_right], size - index_right, comparitor);
}
/// @brief Sort function that will partially sort an array of values up to a limit using a comparitor, using the recursive form of the quicksort algorithm.
/// @tparam type The type of the values to sort.
/// @tparam comparitor_type The type of the comparitor function used to compare pairs of values.
/// @param values The array of values to sort in place.
/// @param size The size of the array of values.
/// @param length_sorted The number of elements to sort.
/// @param comparitor The comparitor function used to determine the order.
template<typename type, typename comparitor_type = default_comparitor_type<type>>
static void sort_partial(type* values, const unsigned long long int size, const unsigned long long int length_sorted, const comparitor_type comparitor = quicksort::comparitor_ascending<type>) {
// Can only sort if there are more than two values.
if (size < 2) {
return;
}
// Always compare against the current value, left is therefore the next value and right the far end.
unsigned long long int index_left = 1;
unsigned long long int index_right = size;
// Move along the indexes swapping as needed until the left and right indexes have met.
while ((index_left < index_right) && ((index_left < length_sorted) || (index_right > length_sorted))) {
// If the comparitor returns true advance left, otherwise swap and advance right.
if (comparitor(values[index_left], values[0])) {
++index_left;
}
else {
--index_right;
quicksort::swap(values[index_left], values[index_right]);
}
}
// Once we've finished looping over the indexes swap the current value into the center by swapping with the value at left.
--index_left;
quicksort::swap(values[index_left], values[0]);
// Now recurse and sort either side of the center.
quicksort::sort_partial(&values[0], index_left + 1, length_sorted, comparitor);
if (index_right < length_sorted) {
quicksort::sort_partial(&values[index_right], size - index_right, length_sorted - index_right, comparitor);
}
}
};
}
#endif // GTL_ALGORITHM_QUICKSORT_HPP