-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuicksort.cpp
More file actions
66 lines (48 loc) · 1.08 KB
/
Quicksort.cpp
File metadata and controls
66 lines (48 loc) · 1.08 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
#include <stdio.h>
#define n 5
void swap(int *A, int pos1, int pos2);
void Quicksort(int *A, int l, int r);
int Partition(int *A, int l, int r);
int main() {
int A[] = {5, 2, 1, 7, 0};
int l = 0, r = n-1;
Quicksort(A, l, r);
// Printando o array ordenado
for(int i = 0; i < n; i++) {
printf("%d ", A[i]);
}
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 = Partition(A, l, r); // s = split position
Quicksort(A, l, s-1);
Quicksort(A, s+1, r);
}
}
int Partition(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));
do {
j = j-1;
} while(!(A[j] <= p));
swap(A, i, j);
} while(!(i >= j));
// Undo last swap when i >= j
swap(A, i, j);
swap(A ,l, j);
return j;
}