diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..bff6b58 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,24 @@ - +require 'pry' +require_relative 'min_heap' # This method uses a heap to sort an array. -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) followed by O(log n) beacause going through entire array for first operation +# Space Complexity: I am attempting to do it in O(1) def heap_sort(list) - raise NotImplementedError, "Method not implemented yet..." + if list.nil? || list.empty? || list.length == 1 + return list + end + + min_heap = MinHeap.new + + until list.empty? + min_heap.add(list.pop) + end + + until min_heap.empty? + retrieved = min_heap.remove + list.push(retrieved) + end + + return list end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..99a7e4d 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -1,3 +1,4 @@ +require 'pry' class HeapNode attr_reader :key, :value @@ -14,18 +15,26 @@ def initialize end # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(log n) bc it's a tree + # Space Complexity: O(1) 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(log n) bc it's a tree + # Space Complexity: O(n log n) bc of recursive calls + def remove + index = 0 + return nil if @store.empty? + + swap(0, -1) + return_value = @store.pop + heap_down(index) + + return return_value.value end @@ -44,28 +53,52 @@ def to_s end # This method returns true if the heap is empty - # Time complexity: ? - # Space complexity: ? + # Time complexity: O(1) - Uncertain if Ruby under the hood implements an array in the strictest sense, then by inference it allocates a certain number of spaces to an array and retrieving the length would be constant. Otherwise it is O(n) due to check each element until it becomes nil. + # Space complexity: O(1) def empty? - raise NotImplementedError, "Method not implemented yet..." + return @store.empty? end private # 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: ? + # 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: O(logn) + # Space complexity: Constant def heap_up(index) - + while index > 0 + parent_index = ((index - 1) / 2) + if @store[parent_index].key > @store[index].key + swap(parent_index, index) + index = parent_index + else + return + end + 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..." + child_index_left = ((index * 2) + 1) + child_index_right = ((index * 2) + 2) + + #if there are no children, return + if @store[child_index_left].nil? + return + elsif @store[child_index_right].nil? + swap(index, child_index_left) + else + if @store[child_index_left].key < @store[child_index_right].key + swap(index, child_index_left) + heap_down(child_index_left) + else + swap(index, child_index_right) + heap_down(child_index_right) + end + end end # If you want a swap method... you're welcome diff --git a/test/min_heap_test.rb b/test/min_heap_test.rb index 186d4c2..7bfe97c 100644 --- a/test/min_heap_test.rb +++ b/test/min_heap_test.rb @@ -78,4 +78,34 @@ # Another assert expect(removed).must_equal "Pasta" end +end + +describe "heap sort" do + it "can handle a null list" do + result = heap_sort(nil) + assert_nil(result) + end + + it "can handle an empty list" do + list = [] + result = heap_sort(list) + + assert_empty(result) + end + + it "can handle a list with one element" do + list = [10] + result = heap_sort(list) + + expect(result).must_equal list + end + + it "can sort a list in ascending order using heap sort" do + list = [5, 3, 1, 7] + min_result = [1, 3, 5, 7] + + sorted_result = heap_sort(list) + + expect(sorted_result).must_equal(min_result) + end end \ No newline at end of file