-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
54 lines (48 loc) · 854 Bytes
/
QuickSort.cpp
File metadata and controls
54 lines (48 loc) · 854 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
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;
#define MAXN 2005
int a[MAXN];
int partition(int low, int high)
{
int pivot = a[low];
int l = low + 1;
int r = high;
while (true)
{
while (a[l] <= pivot && r >= l)
l++;
while (a[r] >= pivot && r >= l)
r--;
if (l >= r)
{
swap(a[r], a[low]);
return r;
}
swap(a[l], a[r]);
}
}
void quickSort(int low, int high)
{
if(low >= high)
return;
if(low + 1 == high)
{
if(a[low] > a[high])
swap(a[low], a[high]);
return;
}
int pivot = partition(low, high); // pivot index
quickSort(low, pivot - 1); // do quickSort for the left half
quickSort(pivot + 1, high); // do quickSort for the right half
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
quickSort(0, n - 1);
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}