-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort
More file actions
60 lines (56 loc) · 1.24 KB
/
QuickSort
File metadata and controls
60 lines (56 loc) · 1.24 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
#include <stdio.h>
void select(int *a, int low, int high) {
int j,k,i,save;
j = high;
while (j > low) {
k = j;
i = j - 1;
while (i >= low) {
if (a[k] < a[i]) k = i;
i--;
}
save = a[j];
a[j] = a[k];
a[k] = save;
j--;
}
}
int partion(int *a, int low,int high) {
int i = low, j = low, save;
while (j < high) {
if (a[j] < a[high]) {
save = a[j];
a[j] = a[i];
a[i] = save;
i++;
}
j++;
}
save = a[high];
a[high] = a[i];
a[i] = save;
return i;
}
void quick_sort(int *a, int m, int low,int high) {
int q;
if (high - low + 1 < m) select(a, low, high); //Если длина меньше m запуск выбор для подпослед;
else {
if (low < high) {
q = partion(a, low, high);
quick_sort(a, m, low, q-1);
quick_sort(a, m, q+1, high);
}
}
}
void quick(int *a,int m, int n) {
quick_sort(a, m, 0, n-1);
}
int main() {
int i, n, m;
scanf("%d %d", &n, &m);
int a[n];
for (i = 0; i < n;i++) scanf("%d", &a[i]);
quick(a, m, n);
for (i = 0; i < n;i++) printf("%d ", a[i]);
return 0;
}