diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..7fb4cb2 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,24 @@ - +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(nlogn) +# Space Complexity: O(n) +def heapsort(list) + return list if list.length <= 1 + + # create heap + heap = MinHeap.new + list.each do |element| + heap.add(element, element) + end + + # sort list by pulling off item from heap + # and replacing current list indices + i = 0 + while i < list.length + list[i] = heap.remove + i += 1 + end + + return list end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..70bc434 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -14,18 +14,36 @@ def initialize end # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(logn), worst case we have to swap through each level of a balanced tree + # meaning the height of the tree which is logn where n is the number + # of nodes in our tree + # Space Complexity: O(logn), due to the recursive calls through the heap_up helper method def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + # insert new node to the bottom of the heap + # by placing it at the end of the array + @store.push(HeapNode.new(key, value)) + + heap_up(@store.length - 1) if @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(logn) since worst case we have to go through each level, + # n being number of nodes in the heap + # Space Complexity: O(1) since not using recursion def remove() - raise NotImplementedError, "Method not implemented yet..." + return if @store.empty? + + # swap last item in heap with smallest value + swap(0, -1) + + # remove smallest value + removedNode = @store.pop + + # maintain heap structure + heap_down() if @store.length > 1 + + return removedNode.value end @@ -44,10 +62,10 @@ 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 true if @store.length == 0 end private @@ -55,19 +73,44 @@ 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), n is halved with each recursive call def heap_up(index) + # base case to exit recursion + return if index == 0 + parentIndex = (index - 1) / 2 + + if @store[parentIndex].key > @store[index].key + swap(index, parentIndex) + heap_up(parentIndex) + 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..." + def heap_down(index=0) + return if @store.empty? + + while ((2 * index) + 1) < @store.length # so long as we have a left child, continue to swap + rightIndex = (2 * index) + 2 + leftIndex = (2 * index) + 1 + smallerIndex = leftIndex + if (rightIndex < @store.length) && (@store[rightIndex].key < @store[leftIndex].key) + smallerIndex = rightIndex + end + + if @store[index].key < @store[smallerIndex].key + break + else + swap(index, smallerIndex) + end + index = smallerIndex + end end + # If you want a swap method... you're welcome def swap(index_1, index_2) temp = @store[index_1] 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 = []