-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_sort.c
More file actions
83 lines (66 loc) · 1.66 KB
/
heap_sort.c
File metadata and controls
83 lines (66 loc) · 1.66 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
77
78
79
80
81
82
83
#include <stdio.h>
/**
* Heapify arr with size n at root
*/
void heapify(int *arr, int n, int root) {
int largest = root;
int left = 2 * root + 1;
int right = 2 * root + 2;
// If our left child is larger than our root
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
// If our right child is larger than our root
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
// If our root is not the largest
if (largest != root) {
// Swap largest child with root
int tmp = arr[root];
arr[root] = arr[largest];
arr[largest] = tmp;
// Recursively call on affected sub-tree
heapify(arr, n, largest);
}
}
/**
* Heapsort main function
*/
void heapsort(int *arr, int n) {
// Start at the last non-leaf node and heapify the array
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for (int i = n - 1; i > 0; i--) {
// Extact the max from the heap and swap with the last element
int tmp = arr[0];
arr[0] = arr[i];
arr[i] = tmp;
// Re-heapify the array
heapify(arr, i, 0);
}
}
/**
* Prints out an array to std out
*/
void printArray(int *arr, int n) {
int i;
for (i = 0; i < n; i++) {
printf("%d", arr[i]);
if (i < n - 1) {
printf(", ");
}
}
printf("\n\n");
}
int main() {
int input_array[] = {9, 8, 2, 4, 10, 5, 7, 6, 3, 1, 7};
int size = 10;
printf("Before:\n");
printArray(input_array, size);
heapsort(input_array, size);
printf("After:\n");
printArray(input_array, size);
return 0;
}