-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.cpp
More file actions
66 lines (46 loc) · 1.14 KB
/
quickSort.cpp
File metadata and controls
66 lines (46 loc) · 1.14 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
66
//ALDO FUSTER TURPIN
#include <iostream>
#include <vector>
using namespace std;
void printArray(const vector <int> &arr) {
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
cout << endl;
}
void swap(vector<int> &arr, const int &i, const int &j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
int partition(vector<int> &arr, int low, int high) {
int pivot = arr[low];
--low;
++high;
while (low < high) {
do ++low; while (arr[low] < pivot);
do --high; while (arr[high] > pivot);
if (low < high) swap(arr, low, high);
}
return high;
}
void quickSort(vector<int> &arr, const int &low, const int &high) {
if (low < high) {
int divisionPoint = partition(arr, low, high);
quickSort(arr, low, divisionPoint);
quickSort(arr, divisionPoint + 1, high);
}
}
void sortArray(vector<int> &arr) {
quickSort(arr, 0, arr.size()-1);
}
int main()
{
int n;
cin >> n;
vector <int> arr(n);
for (int i = 0; i < n; ++i)
cin >> arr[i];
sortArray(arr);
printArray(arr);
return 0;
}