Skip to content

Instantly share code, notes, and snippets.

@fabianbaechli
Created April 22, 2025 13:54
Show Gist options
  • Select an option

  • Save fabianbaechli/8173b15cbb6e869c24131927726755d5 to your computer and use it in GitHub Desktop.

Select an option

Save fabianbaechli/8173b15cbb6e869c24131927726755d5 to your computer and use it in GitHub Desktop.
Heapify(A, i, s)
m = i
// getting left and right children
l = 2 * i + 1
r = 2 * i + 2
// If l exists and is larger than parent:
if l ≤ s && A[l] > A[m]
m = l
// if r exists and is larger than parent:
if r ≤ s && A[r] > A[m]
m = r
// if we have changed m
if i ≠ m
exchange A[i] and A[m]
// propagate downwards on swapped branch
Heapify(A, m, s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment