forked from srbcheema1/Algo_Ds
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsecondhack4.cpp
More file actions
35 lines (31 loc) · 890 Bytes
/
secondhack4.cpp
File metadata and controls
35 lines (31 loc) · 890 Bytes
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
#include "QuickSort.h"
#include "Utils.h"
int get_partition_ind(int input_array[], int left, int right)
{
auto &pivot = input_array[right];
auto next_ind = left;
for (auto i = left; i < right; ++i) {
if (input_array[i] < pivot) {
std::swap(input_array[next_ind++], input_array[i]);
}
}
std::swap(input_array[next_ind], pivot);
return next_ind;
}
void quick_sort_recursive(int * input_array, int left, int right, int &counter)
{
if (left < right) {
const auto pivot = get_partition_ind(input_array, left, right);
quick_sort_recursive(input_array, left, pivot - 1, counter);
quick_sort_recursive(input_array, pivot + 1, right, counter);
#ifdef ENABLE_LOGGING
LOG("Pass %d -> ", counter++);
print_array(input_array, right - left + 1);
#endif
}
}
void quick_sort(int * input_array, int length)
{
auto i = 0;
quick_sort_recursive(input_array, 0, length - 1, i);
}