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
22 changes: 16 additions & 6 deletions lib/heap_sort.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@


# This method uses a heap to sort an array.
# Time Complexity: ?
# Space Complexity: ?
def heap_sort(list)
raise NotImplementedError, "Method not implemented yet..."
end
# Time Complexity: O(nlogn)
# Space Complexity: O(n)
def heapsort(list)

Choose a reason for hiding this comment

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

⚠️ , Space & time complexity?

Also you add things into a heap, but in the internal structure the items are not sorted. They're maintained in heap order with the parent smaller than it's children. You have to REMOVE elements from the heap 1-by-1 to be guaranteed to get it into order.

return list if list.empty?
heap = MinHeap.new

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

list.length.times do |i|
list[i] = heap.remove
end

return list
end
46 changes: 33 additions & 13 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,33 @@ def initialize(key, value)
end

class MinHeap
attr_reader :store

def initialize
@store = []
end

# This method adds a HeapNode instance to the heap
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(logn)
# Space Complexity: O(1)
def add(key, value = key)
Comment on lines +18 to 20

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..."
new_i = @store.length
@store[new_i] = HeapNode.new(key, value)
heap_up(new_i)
return key
end

# This method removes and returns an element from the heap
# maintaining the heap structure
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(logn)
# Space Complexity: O(1)
def remove()
raise NotImplementedError, "Method not implemented yet..."
value = @store[0].value

swap(0, -1)
@store.delete_at(-1)
heap_down(0)
return value
end


Expand All @@ -44,28 +53,39 @@ def to_s
end

# This method returns true if the heap is empty
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(0)
# Space complexity: O(0)
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.empty?
end

private

# This helper method takes an index and
# moves it up the heap, if it is less than it's parent node.
# It could be **very** helpful for the add method.
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(logn)
# Space complexity: O(0)
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.

👍


while index > 0 && @store[index].key < @store[(index - 1)/2].key
swap(index, (index - 1)/2)
index = (index - 1)/2
return if index == 0
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 77 to 80

Choose a reason for hiding this comment

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

This isn't working because you've got to:

  1. Check the both left and right child
    1. And swap with the smaller of the two, if needed
  2. Consider the case where there is a left child and not a right child

raise NotImplementedError, "Method not implemented yet..."
if @store[index * 2 + 1] != nil
while @store[index].key > @store[index * 2 + 1].key
swap(index, index * 2 + 1)
swap(index, index * 2 + 2) if @store[index * 2 + 2] != nil && @store[index].key > @store[index * 2 + 2].key
Comment on lines +83 to +84

Choose a reason for hiding this comment

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

Fixed!

index = index * 2 + 1
return if @store[index * 2 + 1].nil?
end
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