-
Notifications
You must be signed in to change notification settings - Fork 2
/
quickSort.c
50 lines (42 loc) 路 943 Bytes
/
quickSort.c
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
#include <stdio.h>
void troca(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
int particiona(int vet[], int p, int r) {
int i = p;
int j = r + 1;
while (1) {
while (vet[++i] <= vet[p])
if (i == r) break;
while (vet[--j] > vet[p])
if (j == p) break;
if (i >= j) break;
troca(&vet[i], &vet[j]);
}
troca(&vet[p], &vet[j]);
return j;
}
void quick(int vet[], int p, int r) {
if (p >= r) return;
int j = particiona(vet, p, r);
quick(vet, p, j - 1);
quick(vet, j + 1, r);
}
void quickSort(int vet[], int t) {
quick(vet, 0, t - 1);
}
void exibir(int vet[], int t) {
for (int i = 0; i < t; ++i)
printf(" %d", vet[i]);
printf("\n");
}
int main(void) {
int vet[] = {61, 51, 6, 50, 78, 40, 87, 88, 36, 7, 9, 53, 2, 9, 25};
int tam = sizeof(vet) / sizeof(vet[0]);
exibir(vet, tam);
quickSort(vet, tam);
exibir(vet, tam);
return 0;
}