diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..9437258 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,50 @@ # This method uses a heap to sort an array. -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n log n) +# Space Complexity: O(1) def heap_sort(list) - raise NotImplementedError, "Method not implemented yet..." + n = list.size + return list if n < 2 + + # heap construction - reorganize original array into a maximum heap + # proceed from right to left, starting halfway back through the array + # O(n) + i = (n - 1) / 2 + while i >= 0 + heap_down(list, i, n) + i -= 1 + end + + # sort + # remove the largest remaining item from the heap and + # put it into the array position vacated as the heap shrinks + # O(n log n) + while n > 0 + list[0], list[n - 1] = list[n - 1], list[0] + + n -= 1 + heap_down(list, 0, n) + + end + list +end + + +# max heap heap_down method +def heap_down(list, index, n) + while index * 2 + 1 < n + j = index * 2 + 1 # first child + + if j < n - 1 && list[j] < list[j + 1] # greater of two children + j += 1 + end + + if list[index] > list[j] # stop if move will violate heap order + break + end + + list[index], list[j] = list[j], list[index] + index = j + end end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..7910a33 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -14,18 +14,28 @@ def initialize end # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(log n) - worst case, when need heap-up, O(1) - best case + # Space Complexity: O(log n) - worst case, when need heap-up, O(1) - best case def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + new_node = HeapNode.new(key, value) + @store << new_node + heap_up(@store.size-1) end # This method removes and returns an element from the heap # maintaining the heap structure - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(log n) + # Space Complexity: O(log n) def remove() - raise NotImplementedError, "Method not implemented yet..." + return if @store.empty? + if @store.size == 1 + return @store.pop + end + # swap the first and the last element in the underlying array + @store[-1], @store[0] = @store[0], @store[-1] + removed = @store.pop + heap_down(0) + return removed.value end @@ -44,10 +54,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..." + @store.empty? end private @@ -55,17 +65,34 @@ 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(log n) + # Space complexity: O(log n) def heap_up(index) - + # base case + parent_index = (index - 1) / 2 + return if parent_index < 0 || @store[index].key >= @store[parent_index].key + + # swap + @store[index], @store[parent_index] = @store[parent_index], @store[index] + # recursive case + heap_up(parent_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..." + n = @store.size + while index * 2 + 1 < n + j = index * 2 + 1 + + j += 1 if j < n - 1 && @store[j + 1].key < @store[j].key + + break if @store[index].key <= @store[j].key + + @store[index], @store[j] = @store[j], @store[index] + index = j + 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..271e711 100644 --- a/test/heapsort_test.rb +++ b/test/heapsort_test.rb @@ -1,12 +1,12 @@ require_relative "test_helper" -xdescribe "heapsort" do +describe "heapsort" do it "sorts an empty array" do # Arrange list = [] # Act - result = heapsort(list) + result = heap_sort(list) # Assert expect(result).must_equal [] @@ -17,7 +17,7 @@ list = [5] # Act - result = heapsort(list) + result = heap_sort(list) # Assert expect(result).must_equal [5] @@ -28,7 +28,7 @@ list = [5, 27, 3, 16, -50] # Act - result = heapsort(list) + result = heap_sort(list) # Assert expect(result).must_equal [-50, 3, 5, 16, 27]