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
17 changes: 15 additions & 2 deletions lib/heap_sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,18 @@
# Time Complexity: ?
# Space Complexity: ?
def heapsort(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..."
end
return list if list.empty? || list.length == 1

heap = MinHeap.new

list.each do |num|
heap.add(num)
end
list = []
until heap.empty?
removed = heap.remove
list << removed
end
return list
end

68 changes: 61 additions & 7 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,33 @@ def initialize
end

# This method adds a HeapNode instance to the heap
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: 0(1)?
# Space Complexity: o(n)?
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.

👍 The time complexity would be O(log n) because of how you heap_up going up through the levels of the tree.

Similarly because heap_up is recursive the time complexity is O(log n).

raise NotImplementedError, "Method not implemented yet..."
node = HeapNode.new(key, value)
if @store.empty?
@store << node
else
@store << node
current_index = @store.length - 1
@store = heap_up(current_index)
end

end

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

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..."
removed = @store[0].value
@store[0] = @store[@store.length - 1]
parent_node = @store[0]
@store.pop
@store = heap_down(0)
return removed
end


# Used for Testing
def to_s
return "[]" if @store.empty?
Expand All @@ -47,7 +59,7 @@ def to_s
# Time complexity: ?
# Space complexity: ?
def empty?

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.empty?
end

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

Choose a reason for hiding this comment

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

👍 Time & space complexity?

node = @store[index]
parent_index = (index - 1)/2
parent_node = @store[parent_index]
return @store if parent_node.key <= node.key || index == 0

@store[index] = parent_node
@store[parent_index] = node

index = parent_index
return heap_up(index)
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.

This works, but you've overcomplicated some of the if statements.

raise NotImplementedError, "Method not implemented yet..."
parent_node = @store[index]
left_child_index = index * 2 + 1
right_child_index = index * 2 + 2
left_child = @store[left_child_index]
right_child = @store[right_child_index]
if left_child.nil? == false && right_child.nil? == false
return @store if (parent_node.key <= left_child.key && parent_node.key <= right_child.key)

if left_child.key < right_child.key
@store[index] = left_child
@store[left_child_index] = parent_node
index = left_child_index
return heap_down(index)
else
@store[index] = right_child
@store[right_child_index] = parent_node
index = right_child_index
return heap_down(index)
end
elsif left_child.nil? == true && right_child.nil? == true
return @store
elsif left_child.nil? == true && right_child.nil? == false
Comment on lines +108 to +110

Choose a reason for hiding this comment

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

I don't think the right child can't be nil if the left child is.

similarly I don't think the left child can be nil and the right not nil.

return @store if parent_node.key <= right_child.key
@store[index] = right_child
@store[right_child_index] = parent_node
index = right_child_index
return heap_down(index)
else left_child.nil? == false && right_child.nil? == true

Choose a reason for hiding this comment

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

Good catch checking if the right child is past the end of the array and the left isn't.

return @store if parent_node.key <= left_child.key
@store[index] = left_child
@store[left_child_index] = parent_node
index = left_child_index
return heap_down(index)
end
end

# If you want a swap method... you're welcome
Expand Down
2 changes: 1 addition & 1 deletion test/heapsort_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative "test_helper"

xdescribe "heapsort" do
describe "heapsort" do
it "sorts an empty array" do
# Arrange
list = []
Expand Down
1 change: 0 additions & 1 deletion test/min_heap_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
output = heap.to_s

# Assert

expect(output).must_equal "[Donuts, Pizza, Pasta, Soup, Cookies, Cake]"
end

Expand Down