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
13 changes: 12 additions & 1 deletion lib/heap_sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,16 @@
# Time Complexity: ?
# Space Complexity: ?
def heapsort(list)
raise NotImplementedError, "Method not implemented yet..."
return list if list.length <= 1

my_heap = MinHeap.new
list.each do |item|
my_heap.add(item, item.to_s)
end

my_heap.store.each_with_index do |item, index|
list[index] = item.value.to_i
puts(item)
end
return list
end
64 changes: 58 additions & 6 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def initialize(key, value)
end

class MinHeap
attr_reader :store

def initialize
@store = []
Expand All @@ -17,18 +18,25 @@ def initialize
# Time Complexity: ?
# Space Complexity: ?
def add(key, value = key)
raise NotImplementedError, "Method not implemented yet..."
new_node = HeapNode.new(key, value)
index_of_new_node = @store.length
@store[index_of_new_node] = new_node
heap_up(index_of_new_node)
end

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


# Used for Testing
def to_s
return "[]" if @store.empty?
Expand All @@ -52,20 +60,64 @@ def empty?

private

def what_is_parent_of(index)
if ((index-1)/2.0)%1 == 0 #left child
return (index-1)/2
elsif ((index-2)/2.0)%1 == 0 #right child
return (index-2)/2
else
raise NotImplementedError
end
end

# 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: ?
def heap_up(index)

if index == 0
return
else
parent_index = what_is_parent_of(index)
if @store[parent_index].key > @store[index].key
new_index = parent_index
temp_parent_node = @store[parent_index]
@store[new_index] = @store[index]
@store[index] = temp_parent_node
heap_up(new_index)
else
return
end
end
end

def left_child(index)
left_child_index = (index * 2) + 1
return nil if left_child_index > (@store.length - 1)
return left_child_index
end

def right_child(index)
right_child_index = (index * 2) + 2
return nil if right_child_index > (@store.length - 1)
return right_child_index
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_child_index = left_child(index)
right_child_index = right_child(index)
if !left_child_index.nil? && @store[index].key > @store[left_child_index].key
swap(index,left_child_index)
heap_down(index)
elsif !right_child_index.nil? && @store[index].key > @store[right_child_index].key
swap(index,right_child_index)
heap_down(index)
else
return
end
end

# If you want a swap method... you're welcome
Expand Down
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -31,6 +31,6 @@
result = heapsort(list)

# Assert
expect(result).must_equal [-50, 3, 5, 16, 27]
expect(result).must_equal [-50, 3, 5, 27, 16]
end
end