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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The methods you will need to write are:

## HeapSort

The last method to write is `heapsort(list)` which takes an array and uses a `Heap` instance to sort the array. We have discussed this method before. It should look somewhat like insertion sort.
The last method to write is `(list)` which takes an array and uses a `Heap` instance to sort the array. We have discussed this method before. It should look somewhat like insertion sort.

Make certain you also document the expected runtime of each method.

Expand Down
19 changes: 18 additions & 1 deletion lib/heap_sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,22 @@
# 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.

👍 Space/Time Complexity?

raise NotImplementedError, "Method not implemented yet..."
if list.empty?
return []
end

heap = MinHeap.new

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

sorted = []

until heap.empty?
sorted << heap.remove
end


return sorted
end
42 changes: 37 additions & 5 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.

👍 Space/Time Complexity?

raise NotImplementedError, "Method not implemented yet..."
@store << HeapNode.new(key, value)

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 25 to 29

Choose a reason for hiding this comment

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

👍 Space/Time Complexity?

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

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

heap_down(0) unless @store.empty?

removed.value
end


Expand All @@ -47,7 +56,7 @@ def to_s
# Time complexity: ?
# Space complexity: ?
def empty?
raise NotImplementedError, "Method not implemented yet..."
@store.empty?
end

private
Expand All @@ -58,14 +67,37 @@ def empty?
# Time complexity: ?
# Space complexity: ?
def heap_up(index)
Comment on lines 67 to 69

Choose a reason for hiding this comment

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

👍 Space/Time Complexity?


if index == 0
return
end

head = (index - 1 ) / 2

if index == 0
return
elsif @store[index].key < @store[head].key
swap(head,index)
heap_up(head)
end

end

# This helper method takes an index and
# moves it up the heap if it's smaller
# than it's parent node.
def heap_down(index)
Comment on lines 85 to 88

Choose a reason for hiding this comment

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

👍 Space/Time Complexity?

Nice work handling all three scenarios here.

raise NotImplementedError, "Method not implemented yet..."
left = (index * 2) + 1
right = (index * 2) + 2
if left >= @store.length - 1
return
end
if @store[right] && @store[right].key < @store[left].key && @store[right].key < @store[index].key
swap(right, index)
heap_down(right)
elsif @store[left].key < @store[index].key
swap(left, index)
heap_down(left)
end
end

# If you want a swap method... you're welcome
Expand Down
8 changes: 4 additions & 4 deletions test/heapsort_test.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require_relative "test_helper"

xdescribe "heapsort" do
describe "heap_sort" do
it "sorts an empty array" do
# Arrange
list = []

# Act
result = heapsort(list)
result = heap_sort(list)

# Assert
expect(result).must_equal []
Expand All @@ -17,7 +17,7 @@
list = [5]

# Act
result = heapsort(list)
result = heap_sort(list)

# Assert
expect(result).must_equal [5]
Expand All @@ -28,7 +28,7 @@
list = [5, 27, 3, 16, -50]

# Act
result = heapsort(list)
result = heap_sort(list)

# Assert
expect(result).must_equal [-50, 3, 5, 16, 27]
Expand Down