function sift_up(heap, i):
   while i > 0 and heap[parent(i)] > heap[i]:

      swap(heap, i, parent(i))
      i = parent(i)

 function sift_down(heap, i):
   smallest = i

   for child in [left(i), right(i)]:
      if child < length(heap) and heap[child] < heap[smallest]:
        smallest = child
   if smallest != i:
      swap(heap, i, smallest)

      sift_down(heap, smallest) # both run in O(log n)
