forked from iiitv/algos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap_sort.c
63 lines (57 loc) · 1.45 KB
/
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
59
60
61
62
63
/*
* C program to Implement Heap sort.
* Time complexity: Average = O( n log( n ) ), worst case complexity = O( n log( n ) ).
* Space complexity: O(1).
*/
#include <stdio.h>
// root = index root of the subtree, a is an array, heapsize = size of heap
void max_heapify(int a[], int root, int heapsize) {
int largest = root;
int l = (2 * root) + 1; // left child
int r = (2 * root) + 2; // Right child
// Check if left child is larger than root.
if ((l < heapsize) && (a[l] > a[root])) {
largest = l;
}
// Check if right child is larger than largest.
if ((r < heapsize) && (a[r] > a[largest])) {
largest = r ;
}
// If root is not the largest.
if (largest != root) {
int tmp = a[root];
a[root] = a[largest];
a[largest] = tmp;
max_heapify(a, largest, heapsize);
}
}
// a is the array.
void heap_sort(int a[], int heapsize) {
int i;
// Building max heap.
for (i = (heapsize / 2) - 1; i >= 0; i--) {
max_heapify(a, i, heapsize);
}
// One by one extract an element from heap
for (i = heapsize - 1; i > 0; i--) {
int tmp = a[i];
a[i] = a[0];
a[0] = tmp;
heapsize--;
// Again build max heap with the reduced array.
max_heapify(a, 0, heapsize);
}
}
int main() {
int i, r;
// Unsorted data
int a[] = {10000, -999, 240, 1111111, 3, 2, 452, -65};
int size = sizeof(a) / sizeof(a[0]);
// Calling heap_sort function
heap_sort(a, size);
printf("After Sorting:\t");
for (i = 0; i < size; i++) {
printf("%d ", a[i]);
}
return 0;
}