project2/p2q2.rb
def sift_down(i)
  child = (i * 2) + 1
  if child >= @heap.length
    return
  end
  if child + 1 < @heap.length
    if @heap[child][1] > @heap[child+1][1]
      child += 1
    end
  end
  if @heap[i][1] > @heap[child][1]
    @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][1] > @heap[child+1][1]
      child += 1
    end
  end
  if @heap[i][1] > @heap[child][1]
    @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 ] [ 1 ] > @heap [ child + 1 ] [ 1 ] child += 1 end end if @heap [ i ] [ 1 ] > @heap [ child ] [ 1 ] @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