-
Notifications
You must be signed in to change notification settings - Fork 34
/
QuickSort
71 lines (56 loc) · 1.14 KB
/
QuickSort
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
71
#include<iostream>
#include "Solution.h"
using namespace std;
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int partition(int input[],int s, int e) {
int x = input[s];
int count = 0;
for(int i=s;i<=e;i++) {
if(x>input[i]) {
count++;
}
}
int pi = s+count;
swap(input[s],input[pi]);
int i=s;
int j=e;
while(i<pi && j>pi) {
if(input[i]<input[pi])
i++;
else if(input[j]>=input[pi]) {
j--;
}
else {
swap(input[i],input[j]);
}
}
return pi;
}
void quickSort1(int input[], int s, int e) {
if(s>=e) {
return;
}
int pi = partition(input,s,e);
quickSort1(input,s,pi-1);
quickSort1(input,pi+1,e);
}
void quickSort(int input[], int size) {
quickSort1(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;
}