-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQUICK_SORT.cpp
38 lines (29 loc) · 909 Bytes
/
QUICK_SORT.cpp
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
#include <print>
#include <span>
int32_t partition(const std::span<int32_t>& nums, const int32_t& low, const int32_t& high) {
int32_t pivot = nums[high], index = low - 1;
for(int32_t j = low; j < high ;++j) {
if(nums[j] <= pivot) {
++index;
std::swap(nums[j], nums[index]);
}
}
++index;
std::swap(nums[high], nums[index]);
return index;
}
void quick_sort(const std::span<int32_t>& nums, const int32_t& low, const int32_t& high) {
if(low >= high) {
return;
}
int32_t pivot = partition(nums, low, high);
quick_sort(nums, low, pivot - 1);
quick_sort(nums, pivot + 1, high);
}
int32_t main() {
std::array nums {-5, -7, 1, -2, 0, -1};
std::println("Before: {:n}", nums);
quick_sort(nums, 0, nums.size() - 1);
std::println("After: {:n}", nums);
return 0;
}