project2/p2q3-alt.rb
def sift_down(i)
    child = (i * 2) + 1
    if child >= @heap.length
      return
    end
    if child + 1 < @heap.length
      if @heap[child][0] > @heap[child+1][0]
        child += 1
      end
    end
    if @heap[i][0] > @heap[child][0]
      @heap[child], @heap[i] = @heap[i], @heap[child]
      sift_down(child)
    end
  end
# Sift down the element at index i
Sift down the element at index i
Sift down the element at index i
def sift_down(i)
    child = (i * 2) + 1
    if child >= @heap.length
      return
    end
    if child + 1 < @heap.length
      if @heap[child][0] > @heap[child+1][0]
        child += 1
      end
    end
    if @heap[i][0] > @heap[child][0]
      @heap[child], @heap[i] = @heap[i], @heap[child]
      sift_down(child)
    end
  end
def sift_down ( i ) child = ( i * 2 ) + 1 if child >= @heap . length return end if child + 1 < @heap . length if @heap [ child ] [ 0 ] > @heap [ child + 1 ] [ 0 ] child += 1 end end if @heap [ i ] [ 0 ] > @heap [ child ] [ 0 ] @heap [ child ] , @heap [ i ] = @heap [ i ] , @heap [ child ] sift_down ( child ) end end
Sift down the element at index i
Sift down the element at index i
i