-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortMethods.c
More file actions
72 lines (61 loc) · 1.77 KB
/
SortMethods.c
File metadata and controls
72 lines (61 loc) · 1.77 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
#include "SortMethods.h"
#include <stdio.h>
void resetHeap(int *vector, int cur_index, int vec_size, int (*compare)(int, int)){
int aux = 0, left_son = 0, right_son = 0, father_index = 0;
while( 1 ){
left_son = 2 * cur_index;
right_son = (2 * cur_index) + 1;
if(left_son <= vec_size && (*compare)(vector[left_son], vector[cur_index]))
father_index = left_son;
else
father_index = cur_index;
if(right_son <= vec_size && (*compare)(vector[right_son], vector[father_index]))
father_index = right_son;
if(father_index != cur_index){
aux = vector[cur_index];
vector[cur_index] = vector[father_index];
vector[father_index] = aux;
cur_index = father_index;
}
else
return;
}
}
void buildHeap(int *vector, int vec_size, int (*compare)(int, int)){
int i = 0;
for(i = (vec_size/2); i > 0; i--){
resetHeap(vector, i, vec_size, compare);
}
}
void heapsort(int *vector, int vec_size, int (*compare)(int, int)){
int i = 0, aux = 0;
buildHeap(vector, vec_size, compare);
for(i = vec_size; i > 1; i--){
aux = vector[1];
vector[1] = vector[i];
vector[i] = aux;
resetHeap(vector, 1, i-1, compare);
}
}
int partition(int *vector, int vec_begin, int vec_end, int (*compare)(int, int)){
int pivot = vec_end, i = vec_begin - 1, j, aux;
for(j = vec_begin; j < vec_end; j++){
if((*compare)(vector[j], vector[pivot])){
i += 1;
aux = vector[i];
vector[i] = vector[j];
vector[j] = aux;
}
}
aux = vector[i + 1];
vector[i + 1] = vector[j];
vector[j] = aux;
return i + 1;
}
void quicksort(int *vector, int vec_begin, int vec_end, int (*compare)(int, int)){
if(vec_begin < vec_end){
int pivot = partition(vector, vec_begin, vec_end, compare);
quicksort(vector, vec_begin, pivot-1, compare);
quicksort(vector, pivot+1, vec_end, compare);
}
}