diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index 6b692e4..4bb6501 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -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 \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..c8cf121 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -8,6 +8,7 @@ def initialize(key, value) end class MinHeap + attr_reader :store def initialize @store = [] @@ -17,7 +18,10 @@ 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 @@ -25,10 +29,14 @@ def add(key, value = key) # 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? @@ -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 diff --git a/test/heapsort_test.rb b/test/heapsort_test.rb index 34402ac..b0fcad7 100644 --- a/test/heapsort_test.rb +++ b/test/heapsort_test.rb @@ -1,6 +1,6 @@ require_relative "test_helper" -xdescribe "heapsort" do +describe "heapsort" do it "sorts an empty array" do # Arrange list = [] @@ -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 \ No newline at end of file