-
Notifications
You must be signed in to change notification settings - Fork 1
/
qsort.cpp
98 lines (59 loc) · 1.38 KB
/
qsort.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "MakeCodeAwsome.cpp"
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b - temp;
}
void sort(int arr[], int start, int end){
int size = end-start+1;
write(); write();
deb(size);
deb(start);
deb(end);
// if(size <= 1) return;
int gtr[size];
int lsr[size];
int i, ls = 0, gs = 0;
int fi = 0;
for(i = start; i <= end; i++){
if(arr[i] >= arr[start]){
gtr[gs++] = arr[i];
}
else{
lsr[ls++] = arr[i];
}
}
if(ls > 1) sort(lsr, 0, ls);
if(gs > 2) sort(gtr, 1, gs);
// for(int p = 0; p < ls; p++){
// for(int q = 0; q-1 < ls; q++){
// if(lsr[q] > lsr[q+1]) swap(lsr[q], lsr[q+1]);
// }
// }
// for(int p = 0; p < gs; p++){
// for(int q = 0; q < gs - 1; q++){
// if(gtr[q] > gtr[q+1]) swap(gtr[q], gtr[q+1]);
// }
// }
for(i = 0; i < ls; i++){
arr[fi] = lsr[i];
fi++;
}
for(i = 0; i < gs; i++){
arr[fi] = gtr[i];
fi++;
}
}
int main(){
write("Enter size of array");
int n;
read(n);
int arr[n];
write("Enter the array elements");
int i;
for(i = 0; i < n; i++) read(arr[i]);
sort(arr, 0, n-1);
write("final");
for(i = 0; i < n; i++) write(arr[i]);
return 0;
}