MCPcopy Create free account
hub / github.com/PrajaktaSathe/Java / heapify

Method heapify

Programs/HeapSort.java:23–45  ·  view source on GitHub ↗
(int arr[], int n, int i)

Source from the content-addressed store, hash-verified

21 // To heapify a subtree rooted with node i which is
22 // an index in arr[]. n is size of heap
23 void heapify(int arr[], int n, int i) {
24 int largest = i; // Initialize largest as root
25 int l = 2 * i + 1; // left = 2*i + 1
26 int r = 2 * i + 2; // right = 2*i + 2
27
28 // If left child is larger than root
29 if (l < n && arr[l] > arr[largest])
30 largest = l;
31
32 // If right child is larger than largest so far
33 if (r < n && arr[r] > arr[largest])
34 largest = r;
35
36 // If largest is not root
37 if (largest != i) {
38 int swap = arr[i];
39 arr[i] = arr[largest];
40 arr[largest] = swap;
41
42 // Recursively heapify the affected sub-tree
43 heapify(arr, n, largest);
44 }
45 }
46
47 /* A utility function to print array of size n */
48 static void printArr(int arr[]) {

Callers 1

sortMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected