-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.cpp
More file actions
40 lines (35 loc) · 780 Bytes
/
quickSort.cpp
File metadata and controls
40 lines (35 loc) · 780 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
#include<bits/stdc++.h>
using namespace std;
int partition(vector<int> &v, int low, int high){
int pivot = v[high];
int i = low - 1;
for(int j = low; j <= high - 1; j++){
if(v[j] <= pivot){
i++;
swap(v[i], v[j]);
}
}
swap(v[i + 1], v[high]);
return (i + 1);
}
void quickSort(vector<int> &v, int low, int high){
if(low < high){
int pi = partition(v, low, high);
quickSort(v, low, pi - 1);
quickSort(v, pi + 1, high);
}
}
int main()
{
int n; cin >> n;
vector<int> v(n);
for(int i = 0; i < n; i++){
cin >> v[i];
}
quickSort(v, 0, n - 1);
cout << "Sorted array: ";
for(int i = 0; i < n; i++){
cout << v[i] << ' ';
}
cout << endl;
}