-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path104-heap_sort.c
More file actions
84 lines (76 loc) · 1.85 KB
/
104-heap_sort.c
File metadata and controls
84 lines (76 loc) · 1.85 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
84
#include "sort.h"
/**
* _swap - swap between 2 pointers of int
*
* @a: first int pointer
* @b: second int pointer
* Return: void
*/
void _swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
/**
* heapify - build a heap tree where the head is bigger of value branches
* @array: unsorted array of integer
* @size: size of the array
* @i: index of starter head
* @print_size: size of the array to print
*
* Return: void
*/
void heapify(int *array, size_t size, int i, size_t print_size)
{
int head = i; /* head to compare -> big number */
int left = 2 * i + 1; /* left branch */
int right = 2 * i + 2; /* right branch */
/* left exist && left branch > head */
if (left < (int)size && array[left] > array[head])
head = left;
/* right exist && right branch > head */
if (right < (int)size && array[right] > array[head])
head = right;
/* if the head needs to change */
if (head != i)
{
/* if the value is the same, don't swap */
_swap(&array[i], &array[head]);
print_array(array, print_size);
/* we need to heapify all the tree again */
heapify(array, size, head, print_size);
}
}
/**
* heap_sort - heap sort algorithm
* @array: unsorted array of integer
* @size: size of the array
*
* Return: void
*/
void heap_sort(int *array, size_t size)
{
int first_head, i;
size_t print_size = size;
if (size <= 1 || !array)
return;
/* calculate the index of the first_head */
first_head = (int)(size / 2 - 1);
/* heapify the array */
for (i = first_head; i >= 0; i--)
heapify(array, size, i, print_size);
/* extract each element from heap */
for (i = size - 1; i >= 0; i--)
{
/* if the index is different from 0 */
/* if it is equals value, we need to change */
if (i != 0)
{
_swap(&array[0], &array[i]);
print_array(array, print_size);
heapify(array, i, 0, print_size); /* we need to heapify again in 0 */
}
}
}