MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / heapify

Function heapify

CPP/sorting/heap_sort.cpp:5–25  ·  view source on GitHub ↗

function to heapify a subtree. Here 'i' is the index of root node in array a[], and 'n' is the size of heap. */

Source from the content-addressed store, hash-verified

3/* function to heapify a subtree. Here 'i' is the
4index of root node in array a[], and 'n' is the size of heap. */
5void heapify(int a[], int n, int i)
6{
7 int largest = i; // Initialize largest as root
8 int left = 2 * i + 1; // left child
9 int right = 2 * i + 2; // right child
10 // If left child is larger than root
11 if (left < n && a[left] > a[largest])
12 largest = left;
13 // If right child is larger than root
14 if (right < n && a[right] > a[largest])
15 largest = right;
16 // If root is not largest
17 if (largest != i) {
18 // swap a[i] with a[largest]
19 int temp = a[i];
20 a[i] = a[largest];
21 a[largest] = temp;
22
23 heapify(a, n, largest);
24 }
25}
26/*Function to implement the heap sort*/
27void heapSort(int a[], int n)
28{

Callers 1

heapSortFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected