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.

👍

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
41 changes: 36 additions & 5 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,22 @@ 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.

👍

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.

👍 , noting you're missing time/space complexity

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

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

heap_down(0) if [email protected]?
return removed.value
end


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

Choose a reason for hiding this comment

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

👍 , noting you're missing time/space complexity

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

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

Choose a reason for hiding this comment

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

👍 , noting you're missing time/space complexity


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
# moves it up the heap if it's smaller
# than it's parent node.
def heap_down(index)
Comment on lines 76 to 79

Choose a reason for hiding this comment

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

👍 , noting you're missing time/space complexity

raise NotImplementedError, "Method not implemented yet..."
left_child = (index * 2) + 1
right_child= (index * 2) + 2

return if !@store[left_child] && !@store[right_child]

if @store[left_child].nil?
smallest_index = right_child
elsif @store[right_child].nil?
smallest_index = left_child
elsif @store[left_child].key < @store[right_child].key
smallest_index = left_child
else
smallest_index = right_child
end

if @store[index].key > @store[smallest_index].key
swap(index, smallest_index)
end

return heap_down(smallest_index)
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 "heapsort" 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