-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
59 lines (52 loc) · 1.12 KB
/
main.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
#include <iostream>
using namespace std;
int partition(int input[], int start, int end)
{
int count = 0;
int pivot = input[start];
for (int i = start + 1; i <= end; i++ )
{
if (input[i] <= pivot)
{
count++;
}
}
int pivotIndex = start + count;
input[start] = input[pivotIndex];
input[pivotIndex] = pivot;
int i = start;
int j = end;
while (i <= pivotIndex && j >= pivotIndex)
{
if(input[i] <= pivot)
i++;
else if(input[j] > pivot)
j--;
else
{
swap(input[i], input[j]);
i++;
j--;
}
}
return pivotIndex;
}
void quickSort(int input[], int start, int end)
{
if(start >= end)
return;
int p = partition(input, start, end);
quickSort(input, start, p - 1);
quickSort(input, p + 1, end);
}
int main()
{
int input[] = {6, 4, 3, 3, 2, 1};
int size = sizeof(input)/ sizeof(int);
quickSort(input, 0, size - 1);
for (int i = 0; i < size; i++)
{
cout << input[i] << " ";
}
cout << endl;
}