Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dani - Leaves #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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: 17 additions & 5 deletions lib/heap_sort.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@

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(n)
# Space Complexity: O(n)
def heapsort(list)
heap = MinHeap.new()

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

result = []
while !heap.empty?
node = heap.remove
result.push(node)
end

return result
end
64 changes: 51 additions & 13 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,28 @@ def initialize
end

# This method adds a HeapNode instance to the heap
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(1)
# Space Complexity: O(1)
def add(key, value = key)
raise NotImplementedError, "Method not implemented yet..."
new_node = HeapNode.new(key, value)
@store.push(new_node)

index = @store.length - 1
heap_up(index)
end

# This method removes and returns an element from the heap
# maintaining the heap structure
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def remove()
raise NotImplementedError, "Method not implemented yet..."
if [email protected]?
del = @store.first
@store[0] = @store.last
@store = @store[0...-1]
heap_down(0)
end
return del.value
end


Expand All @@ -44,28 +54,56 @@ 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 @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(1)
# Space complexity: O(1)
def heap_up(index)

i = index

while i != 0
parent_i = get_parent_index(i)
p_node = @store[parent_i]
a_node = @store[i]
if p_node.key > a_node.key
swap(parent_i, i)
end
i = parent_i
end
end

def get_parent_index(index)
return (index - 1) / 2
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..."
left_i = index * 2 + 1
right_i = index * 2 + 2

if right_i < @store.length
min_child = @store[left_i].key < @store[right_i].key ? left_i : right_i

if @store[index].key > @store[min_child].key
swap(index, min_child)
heap_down(min_child)
end
elsif left_i < @store.length
if @store[index].key > @store[left_i].key
swap(index, left_i)
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