-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAP2quick.cpp
More file actions
76 lines (56 loc) · 1.39 KB
/
AP2quick.cpp
File metadata and controls
76 lines (56 loc) · 1.39 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
71
72
73
74
75
76
#include <stdio.h>
void swap(int *a, int pos1, int pos2);
void Quicksort(int *a, int l, int r);
int HoarePartition(int *a, int l, int r);
int main() {
int c;
scanf("%d", &c);
int n[c];
for(int i = 0; i < c; i++) {
scanf("%d", &n[i]);
int array[n[i]];
int l = 0, r = (n[i] - 1);
for(int j = 0; j < n[i]; j++) {
scanf("%d", &array[j]);
}
Quicksort(array, l, r);
for(int j = 0; j < n[i]; j++) {
printf("%d ", array[j]);
}
printf("\n");
}
return 0;
}
void swap(int *a, int pos1, int pos2) {
int aux = 0;
aux = a[pos1];
a[pos1] = a[pos2];
a[pos2] = aux;
}
void Quicksort(int *a, int l, int r) {
int s = 0;
if(l < r) {
s = HoarePartition(a, l, r);
Quicksort(a, l, s - 1);
Quicksort(a, s + 1, r);
}
}
int HoarePartition(int *a, int l, int r) {
int j, p, i;
p = a[l];
i = l; // incrementa
j = r+1; // decrementa
do {
do {
i = i+1;
} while(!(a[i] <= p || i >= r)); // para decrescente troca o >= pra <= no a[i] (o i deixa como ta)
do {
j = j-1;
} while(!(a[j] >= p)); // pra decrescente troca o <= pra >=
swap(a, i, j);
} while(!(i >= j));
// Undo last swap when i >= j
swap(a, i, j);
swap(a ,l, j);
return j;
}