-
Notifications
You must be signed in to change notification settings - Fork 127
/
quick-sort.cpp
40 lines (38 loc) · 848 Bytes
/
quick-sort.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
#include <bits/stdc++.h>
using namespace std;
int partition(int a[], int start, int end)
{
int x = a[start], i = start, k = start + 1, temp;
for (; k <= end; k++)
{
if (a[k] <x) // change sign here for ascending/descending(it will sort in ascending order by default)
{
i++;
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
a[start] = a[i];
a[i] = x;
return i;
}
void quick_sort(int a[], int start, int end)
{
if (start >= end)
return;
int i = partition(a, start, end);
quick_sort(a, start, i);
quick_sort(a, i + 1, end);
}
int main()
{
int a[] = {4, 1, 3, 9, 7};
int size = sizeof(a) / sizeof(a[0]);
quick_sort(a, 0, size - 1);
for (int i = 0; i < size; i++)
{
cout<<a[i]<<" ";
}
return 0;
}