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

require_relative "min_heap"

# 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(nlogn)
# Space Complexity: O(n)
def heapsort(list)
return list if list.length <= 1

# create heap
heap = MinHeap.new
list.each do |element|
heap.add(element, element)
end

# sort list by pulling off item from heap
# and replacing current list indices
i = 0
while i < list.length
list[i] = heap.remove
i += 1
end

return list
end
69 changes: 56 additions & 13 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,36 @@ def initialize
end

# This method adds a HeapNode instance to the heap
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(logn), worst case we have to swap through each level of a balanced tree
# meaning the height of the tree which is logn where n is the number
# of nodes in our tree
# Space Complexity: O(logn), due to the recursive calls through the heap_up helper method
def add(key, value = key)
raise NotImplementedError, "Method not implemented yet..."
# insert new node to the bottom of the heap
# by placing it at the end of the array
@store.push(HeapNode.new(key, value))

heap_up(@store.length - 1) if @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(logn) since worst case we have to go through each level,
# n being number of nodes in the heap
# Space Complexity: O(1) since not using recursion
def remove()
raise NotImplementedError, "Method not implemented yet..."
return if @store.empty?

# swap last item in heap with smallest value
swap(0, -1)

# remove smallest value
removedNode = @store.pop

# maintain heap structure
heap_down() if @store.length > 1

return removedNode.value
end


Expand All @@ -44,30 +62,55 @@ 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?
raise NotImplementedError, "Method not implemented yet..."
return true if @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(logn)
# Space complexity: O(logn), n is halved with each recursive call
def heap_up(index)
# base case to exit recursion
return if index == 0

parentIndex = (index - 1) / 2

if @store[parentIndex].key > @store[index].key
swap(index, parentIndex)
heap_up(parentIndex)
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)
raise NotImplementedError, "Method not implemented yet..."
def heap_down(index=0)
return if @store.empty?

while ((2 * index) + 1) < @store.length # so long as we have a left child, continue to swap
rightIndex = (2 * index) + 2
leftIndex = (2 * index) + 1
smallerIndex = leftIndex
if (rightIndex < @store.length) && (@store[rightIndex].key < @store[leftIndex].key)
smallerIndex = rightIndex
end

if @store[index].key < @store[smallerIndex].key
break
else
swap(index, smallerIndex)
end
index = smallerIndex
end
end


# If you want a swap method... you're welcome
def swap(index_1, index_2)
temp = @store[index_1]
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