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


# This method uses a heap to sort an array.
# Time Complexity: ?
# Space Complexity: ?
def heap_sort(list)
raise NotImplementedError, "Method not implemented yet..."
# Time Complexity: O(n * log(n)) where n is the number of elements.
# Space Complexity: O(log(n)) worsecase because of recursion inside of heapify?
# list = [5, 27, 3, 16, -50]
def heapsort(list)
Comment on lines +4 to +7

Choose a reason for hiding this comment

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

👍 Really nice. However you're using a lot of one letter variable names.

return list if list.empty? || list.length == 1

# i = the parent of the last element
i = (list.size - 1) / 2
# initial heapify of list
while i >= 0
heapify(list, i, list.length)
i -= 1
end

# list = [-50, 5, 3, 16, 27 ]
i = 0
j = list.length - 1
while j >= 0
swap(i, j, list)
j -= 1
heapify(list, 0, j)
end

return list
end

def swap(index1, index2, list)
temp = list[index1]
list[index1] = list[index2]
list[index2] = temp
end

def heapify(list, index, size)
# index here will be the "root" node
largest = index
left = 2 * index + 1
right = 2 * index + 2

# check if left child is greater than parent
if left < size && list[left] > list[largest]
largest = left
end

# also check if right child is greater than parent
if right < size && list[right] > list[largest]
largest = right
end

# if the largest no longer equals it's original value
if largest != index
# swap the root and the found largest value
swap(largest, index, list)
# make sure the order is still maintained for the children of that new index
heapify(list, largest, size)
end
end
55 changes: 42 additions & 13 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,27 @@ def initialize
end

# This method adds a HeapNode instance to the heap
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(log(n))
# Space Complexity: O(log(n) due to the stack call in heap_up, could be O(1) using a loop
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..."
node = HeapNode.new(key, value)
@store.push(node)
heap_up(@store.length - 1)
end

# This method removes and returns an element from the heap
# maintaining the heap structure
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(log(n)) - where n is the number of elements and log(n) reps the levels
# Space Complexity: O(log(n) due to the stack call in heap_down, could be O(1) using a loop
def remove()
Comment on lines +27 to 29

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..."
if @store.length == 0
raise "the heap is empty"
end

swap(0, @store.length - 1)
removed = @store.pop
heap_down(0)
return removed.value
end


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

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

# 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(log(n)) - n for number of elements. log(n) represents the number of levels.
# Space complexity: O(log(n)) - the stack for the resursive calls could be as tall as the number of levels.
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.

👍


return if index == 0
parent_idx = (index - 1) / 2
if @store[parent_idx].key > @store[index].key
swap(parent_idx, index)
heap_up(parent_idx)
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 78 to 81

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..."
left = 2 * index + 1
right = 2 * index + 2
if @store[left] && @store[right] # if both left and right are not nil
child_idx = @store[left].key > @store[right].key ? right : left
elsif @store[left] # there's no case where the left would be null and the right have a value
child_idx = left
else # if they're both null stop the function and return.
return
end

if @store[child_idx].key < @store[index].key
swap(child_idx, index)
heap_down(child_idx)
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