-
Notifications
You must be signed in to change notification settings - Fork 0
/
104-heap_sort.c
58 lines (53 loc) · 1.03 KB
/
104-heap_sort.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
51
52
53
54
55
56
57
58
#include "sort.h"
/**
* heap_sort - Sort an array of integers
*
* @array: The array
* @size: The size of the @array
*/
void heap_sort(int *array, size_t size)
{
int x;
int temp;
if (size < 2)
return;
for (x = size / 2 - 1; x >= 0; x--)
heapify(array, size, (size_t)x, size);
for (x = size - 1; x >= 0; x--)
{
temp = array[x];
array[x] = array[0];
array[0] = temp;
if (x != 0)
print_array(array, size);
heapify(array, (size_t)x, 0, size);
}
}
/**
* heapify - Function for array heap tree
*
* @array: The array
* @s: The size
* @root: The index of subtree
* @size: The size
*/
void heapify(int *array, size_t s, size_t root, size_t size)
{
size_t max, left, right;
int temp;
max = root;
left = (root * 2) + 1;
right = (root * 2) + 2;
if (left < s && array[left] > array[max])
max = left;
if (right < s && array[right] > array[max])
max = right;
if (max != root)
{
temp = array[root];
array[root] = array[max];
array[max] = temp;
print_array(array, size);
heapify(array, s, max, size);
}
}