-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort.cpp
More file actions
45 lines (32 loc) · 828 Bytes
/
quicksort.cpp
File metadata and controls
45 lines (32 loc) · 828 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
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <vector>
#include "util.h"
void partition(std::vector<int> & arr, int left, int right) {
if (left >= right) return;
std::cout << "arr: " << arr << "\n";
int i = left;
int j = right;
int pivotIndex = (left + right) / 2;
int pivot = arr[pivotIndex];
while (i <= j) {
while (arr[i] < pivot) { ++i; }
while (arr[j] > pivot) { --j; }
if (i > j) break;
std::cout << " swap " << i << " with " << j << std::endl;
std::swap(arr[i], arr[j]);
++i; --j;
}
if (left < j) {
partition(arr, left, j);
}
if (i < right) {
partition(arr, i, right);
}
}
int main(int argc, char const *argv[])
{
std::vector<int> arr {4, 5, 1, 2, 4, 5, 6, 2, 4, 8, 9, 1 };
partition(arr, 0, arr.size() - 1);
std::cout << "result: " << arr << "\n";
return 0;
}