forked from oviiii-m/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort
More file actions
72 lines (58 loc) · 1.25 KB
/
quickSort
File metadata and controls
72 lines (58 loc) · 1.25 KB
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
61
62
63
64
65
66
67
68
69
70
#include<iostream>
using namespace std;
int Partition(int input[], int si, int ei)
{
int count=0;
for(int i=si+1; i<=ei; i++){
if(input[i] < input[si]){
count++;
}
}
int pi = count+si;
int temp=input[pi];
input[pi]=input[si];
input[si]=temp;
int i=si, j=ei;
while(i < pi && j > pi){
if(input[i] < input[pi]){
i++;
}
else if(input[j] >= input[pi]){
j--;
}
else{ //if(input[i] > input[pi] && input[j] < input[pi])
int temp = input[i];
input[i] = input[j];
input[j] = temp;
i++;
j--;
}
}
return pi;
}
void helper(int input[], int si, int ei)
{
if(si >= ei){
return;
}
int pi = Partition(input, si, ei);
helper(input, si, pi-1);
helper(input, pi+1, ei);
}
void quickSort(int input[], int size)
{
helper(input, 0, size-1);
}
int main(){
int n;
cin >> n;
int *input = new int[n];
for(int i = 0; i < n; i++) {
cin >> input[i];
}
quickSort(input, n);
for(int i = 0; i < n; i++) {
cout << input[i] << " ";
}
delete [] input;
}