Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/heap_sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,16 @@
# Time Complexity: ?
# Space Complexity: ?
def heap_sort(list)
Comment on lines 4 to 6

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Time/space complexity?

raise NotImplementedError, "Method not implemented yet..."
heap = MinHeap.new

list.each do |i|
heap.add(i)
end

sorted_array = []

until heap.empty?
sorted_array << heap.remove
end
return sorted_array
end
22 changes: 18 additions & 4 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,24 @@ def initialize
# Time Complexity: ?
# Space Complexity: ?
def add(key, value = key)
Comment on lines 17 to 19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Time/space complexity?

raise NotImplementedError, "Method not implemented yet..."
@store << HeapNode.new(key, value)
return heap_up(@store.length - 1)
end

# This method removes and returns an element from the heap
# maintaining the heap structure
# Time Complexity: ?
# Space Complexity: ?
def remove()
Comment on lines 26 to 28

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Time/space complexity?

raise NotImplementedError, "Method not implemented yet..."
return nil if @store.empty?


removed = @store[0]
swap(0, @store.length - 1)
@store = @store[0...-1]

heap_down(0)
return removed.value
end


Expand All @@ -47,7 +56,7 @@ def to_s
# Time complexity: ?
# Space complexity: ?
def empty?
Comment on lines 56 to 58

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError, "Method not implemented yet..."
return @store.length == 0
end

private
Expand All @@ -58,7 +67,12 @@ def empty?
# Time complexity: ?
# Space complexity: ?
def heap_up(index)

return nil if index == 0
parent = (index -1 ) / 2
if @store[index].key < @store[parent].key
swap(index, parent)
heap_up(parent)
end
end

# This helper method takes an index and
Expand Down