-
Notifications
You must be signed in to change notification settings - Fork 0
/
104-heap_sort.c
51 lines (44 loc) · 980 Bytes
/
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
#include "sort.h"
/**
* stupify - recurrssive heapfiy function
* @array: Array to sort
* @heap: size of heap data
* @i: index
* @size: size of array
*/
void stupify(int *array, int heap, int i, int size)
{
int lar = i, left = 2 * i + 1, right = 2 * i + 2, t;
if (left < heap && array[left] > array[lar])
lar = left;
if (right < heap && array[right] > array[lar])
lar = right;
if (lar != i)
{
t = array[i], array[i] = array[lar], array[lar] = t;
print_array(array, size);
stupify(array, heap, lar, size);
}
}
/**
* heap_sort - Sorts array with heap sort algo
* @array: array to sort
* @size: Size of array to sort
*/
void heap_sort(int *array, size_t size)
{
int i = size / 2 - 1, temp;
if (array == NULL || size < 2)
return;
for (; i >= 0; i--)
stupify(array, size, i, size);
for (i = size - 1; i >= 0; i--)
{
temp = array[0];
array[0] = array[i];
array[i] = temp;
if (i > 0)
print_array(array, size);
stupify(array, i, 0, size);
}
}