diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index 6b692e4..82f9812 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,21 @@ - +require_relative "min_heap" # This method uses a heap to sort an array. -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(nlogn) - loops n times based on length of list, and then logn adding or removing +# Space Complexity: O(nlogn) def heapsort(list) - raise NotImplementedError, "Method not implemented yet..." + return list if list.length <= 1 + + heap = MinHeap.new + + list.each do |i| + heap.add(i) + end + + ordered_list = [] + list.length.times do |i| + ordered_list << heap.remove.value + end + + return ordered_list end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..daae38e 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -13,22 +13,45 @@ def initialize @store = [] end + # helper method to find parent + def find_parent(index) + return (index - 1) / 2 + end + + # helper method to find left child + def left_child(index) + return 2 * index + 1 + end + + # helper method to find right child + def right_child(index) + return 2 * index + 2 + end + # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(logn) depending on the height of the heap + # Space Complexity: O(logn) to hold the recursive stack def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + @store << HeapNode.new(key, value) + heap_up(@store.length-1) 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..." + # Time Complexity: O(logn) depending on the height of the heap + # Space Complexity: O(logn) to hold the recursive stack + def remove + if @store.empty? + return nil + end + + swap(0, @store.length - 1) + result = @store.pop + + heap_down(0) unless @store.empty? + return result end - # Used for Testing def to_s return "[]" if @store.empty? @@ -55,17 +78,42 @@ def empty? # 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) def heap_up(index) + return if index == 0 + parent = find_parent(index) + if @store[parent].key > @store[index].key + swap(parent, index) + heap_up(parent) + 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..." + return if index >= @store.length-1 + + left = left_child(index) + right = right_child(index) + + return if !@store[left] && !@store[right] + + smallest = nil + if !@store[left] + smallest = right + elsif !@store[right] + smallest = left + else + smallest = (@store[left].key < @store[right].key) ? left : right + end + + if @store[index].key > @store[smallest].key + swap(index, smallest) + heap_down(smallest) + 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..7ce79b7 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 = [] diff --git a/test/min_heap_test.rb b/test/min_heap_test.rb index 186d4c2..fa1e9a1 100644 --- a/test/min_heap_test.rb +++ b/test/min_heap_test.rb @@ -61,19 +61,19 @@ heap.add(57, "Cake") # Act - removed = heap.remove + removed = heap.remove.value # Assert expect(removed).must_equal "Donuts" # Another Act - removed = heap.remove + removed = heap.remove.value # Another assert expect(removed).must_equal "Pizza" # Another Act - removed = heap.remove + removed = heap.remove.value # Another assert expect(removed).must_equal "Pasta"