Skip to content

Instantly share code, notes, and snippets.

@pulkitnehra
Last active June 22, 2020 04:57
Show Gist options
  • Save pulkitnehra/a3fbff7b24b7bcba400820e543097485 to your computer and use it in GitHub Desktop.
Save pulkitnehra/a3fbff7b24b7bcba400820e543097485 to your computer and use it in GitHub Desktop.
Heapify function in java
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