Skip to content

Instantly share code, notes, and snippets.

@kalub92
Created March 5, 2018 22:30
Show Gist options
  • Save kalub92/b9244a9a3d0e9cd17c0711a9ef75d1f1 to your computer and use it in GitHub Desktop.
Save kalub92/b9244a9a3d0e9cd17c0711a9ef75d1f1 to your computer and use it in GitHub Desktop.
mutating private func heapifyDown() {
var index = 0
while hasLeftChild(index) {
var smallerChildIndex = getLeftChildIndex(index)
if hasRightChild(index) && rightChild(index) < leftChild(index) {
smallerChildIndex = getRightChildIndex(index)
}
if items[index] < items[smallerChildIndex] {
break
} else {
swap(indexOne: index, indexTwo: smallerChildIndex)
}
index = smallerChildIndex
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment