-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheapSort.c
62 lines (60 loc) · 1.39 KB
/
heapSort.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
59
60
61
62
#include <stdio.h>
void printLoop(int *arr, int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
}
// Logic
void heapify(int *arr, int size, int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < size && arr[largest] < arr[left])
{
largest = left;
}
if (right < size && arr[largest] < arr[right])
{
largest = right;
}
if (largest != i)
{
// swap
int temp = arr[largest];
arr[largest] = arr[i];
arr[i] = temp;
heapify(arr, size, largest);
}
}
void heapSort(int *arr, int size)
{
// to do a heap sort there two steps involved
// 1st step to create a heap using heapify {O(n)}
// 2nd step is to delete the root node n times {o(nlog(n))}
// creating a max heap
for (int i = (size / 2) - 1; i >= 0; i--)
{
heapify(arr, size, i);
}
// deleting a max heap and storing the result in the same array
for (int i = size - 1; i >= 0; i--)
{
// swaping first (root) node with last node in heap
int temp = arr[i];
arr[i] = arr[0];
arr[0] = temp;
// again creating a max heap
heapify(arr, i, 0);
}
}
int main()
{
int arr[] = {5, 6, 3, 9, 4, 1, 1, 2, 7, 4};
int size = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, size);
printLoop(arr, size);
return 0;
}