-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSortAlgo
38 lines (32 loc) · 898 Bytes
/
QuickSortAlgo
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 <bits/stdc++.h>
using namespace std;
int partition(vector<int> &arr, int low, int high) {
int pivot = arr[low];
int i = low;
int j = high;
while (i < j) {
while (arr[i] <= pivot && i < high) i++;
while (arr[j] > pivot) j--;
if (i < j) swap(arr[i], arr[j]);
}
swap(arr[low], arr[j]);
return j;
}
void quickSort(vector<int> &arr, int low, int high) {
if (low < high) {
int pIndex = partition(arr, low, high);
quickSort(arr, low, pIndex - 1);
quickSort(arr, pIndex + 1, high);
}
}
int main() {
vector<int> arr = {4, 6, 2, 5, 7, 9, 1, 3};
cout << "Before using quick sort: ";
for (int num : arr) cout << num << " ";
cout << endl;
quickSort(arr, 0, arr.size() - 1);
cout << "After using quick sort: ";
for (int num : arr) cout << num << " ";
cout << endl;
return 0;
}