Last active
June 22, 2020 04:57
-
-
Save pulkitnehra/a3fbff7b24b7bcba400820e543097485 to your computer and use it in GitHub Desktop.
Heapify function in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void heapify(int arr[], int n, int i) { | |
// Find largest among root, left child and right child | |
int largest = i; | |
int left = 2 * i + 1; | |
int right = 2 * i + 2; | |
if (left < n && arr[left] > arr[largest]) | |
largest = left; | |
if (right < n && arr[right] > arr[largest]) | |
largest = right; | |
// Swap and continue heapifying if root is not largest | |
if (largest != i) { | |
swap(&arr[i], &arr[largest]); | |
heapify(arr, n, largest); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment