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
63 changes: 58 additions & 5 deletions lib/heap_sort.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,61 @@
require "pry"
# This method uses a heap to sort an array.
# Time Complexity: O (n log n) where n is the number of elements in the heap
# Space Complexity: O (1)

# Adapted from diagrams and pseudocode in https://medium.com/basecs/heapify-all-the-things-with-heap-sort-55ee1c93af82

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

Choose a reason for hiding this comment

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

Really nice work building an O(1) space complexity heapsort.

limit = list.length
build_max_heap(list)

while limit > 0 # O(n)
swap(list, 0, limit - 1)
limit -= 1
heapify(list, 0, limit) # O(log n)
end

return list
end

def build_max_heap(list)
index = list.length/2 - 1
while index >= 0
heapify(list, index, list.length)
index -= 1
end
end


def heapify(list, index, limit)
root = index

while index < limit
left = (2 * index) + 1
right = (2 * index) + 2

if left >= limit && right >= limit
return
elsif right >= limit
max = left
else
max = [left, right].max_by {|x| list[x]}
end

if list[max] > list[root]
root = max
else
return
end

swap(list, root, index)

index = root
end
end

def swap(list, index_1, index_2)
temp = list[index_1]
list[index_1] = list[index_2]
list[index_2] = temp
end
49 changes: 36 additions & 13 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,23 @@ def initialize
end

# This method adds a HeapNode instance to the heap
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O (log n) where n is the number of nodes in the store
# Space Complexity: O(1)

Choose a reason for hiding this comment

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

Since heap_up is recursive, you have some space complexity here. This should also be O(log n)

def add(key, value = key)
raise NotImplementedError, "Method not implemented yet..."
new_node = HeapNode.new(key, value)
@store << new_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 nodes in the store
# Space Complexity: O(1)
def remove()
raise NotImplementedError, "Method not implemented yet..."
swap(0, @store.length - 1)
removed_node = @store.pop
heap_down(0)
return removed_node.value
end


Expand All @@ -47,25 +52,43 @@ def to_s
# Time complexity: ?
# Space complexity: ?
def empty?
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(log n) where n is the number of nodes in the store
# Space complexity: O(1)
def heap_up(index)

return if index == 0
parent_index = (index - 1) / 2
if @store[index].key < @store[parent_index].key
swap(index, parent_index)
heap_up(parent_index)
else
return
end
end

# This helper method takes an index and
# moves it up the heap if it's smaller
# than it's parent node.
# moves it down the heap
# Time complexity: O (log n) where n is the number of nodes in the store
# Space complexity: O(1)
def heap_down(index)
raise NotImplementedError, "Method not implemented yet..."
left = (2 * index) + 1
right = (2 * index) + 2
if !@store[left] && !@store[right]
return
elsif !@store[right]
min = left
else
min = [left, right].min_by {|x| @store[x].key}
end
swap(index, min)
heap_down(min)
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