forked from projectX-india/madlab-hack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.cpp
60 lines (47 loc) · 1.08 KB
/
QuickSort.cpp
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
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define mod 1000000007
#define Pi 3.1415926536
#define vec vector<int>
#define pb emplace_back
#define all(v) v.begin(), v.end()
#define fastIO {ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);}
#define get_unique(v) {sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());}
void swap(int* a, int* b){
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high){
int pivot = arr[high];
int i = low;
for (int j = low; j < high; j++){
if (arr[j] < pivot){
swap(&arr[i], &arr[j]);
i++;
}
}
swap(&arr[i], &arr[high]);
return i;
}
void quickSort(int arr[], int low, int high){
if (low < high){
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
signed main(){
//fastIO
int n = 10;
int arr[n];
for(int i=0; i<n; i++)
arr[i] = n-i;
quickSort(arr, 0, n);
for(int i=0; i<n; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
}