diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..596e3ef 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -4,5 +4,16 @@ # Time Complexity: ? # Space Complexity: ? def heap_sort(list) - raise NotImplementedError, "Method not implemented yet..." + heap = MinHeap.new + + list.each do |i| + heap.add(i) + end + + sorted_array = [] + + until heap.empty? + sorted_array << heap.remove + end + return sorted_array end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..f8a149d 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -17,7 +17,8 @@ def initialize # Time Complexity: ? # Space Complexity: ? def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + @store << HeapNode.new(key, value) + return heap_up(@store.length - 1) end # This method removes and returns an element from the heap @@ -25,7 +26,13 @@ def add(key, value = key) # Time Complexity: ? # Space Complexity: ? def remove() - raise NotImplementedError, "Method not implemented yet..." + return nil if @store.empty? + + swap(0, @store.length - 1) + removed = @store.pop + + heap_down(0) if !@store.empty? + return removed.value end @@ -47,7 +54,7 @@ def to_s # Time complexity: ? # Space complexity: ? def empty? - raise NotImplementedError, "Method not implemented yet..." + return @store.empty? end private @@ -58,14 +65,38 @@ def empty? # Time complexity: ? # Space complexity: ? def heap_up(index) - + return nil if index == 0 + parent = (index -1 ) / 2 + if @store[index].key < @store[parent].key + swap(index, parent) + 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..." + left_child = (index * 2) + 1 + right_child= (index * 2) + 2 + + return if !@store[left_child] && !@store[right_child] + + if @store[left_child].nil? + smallest_index = right_child + elsif @store[right_child].nil? + smallest_index = left_child + elsif @store[left_child].key < @store[right_child].key + smallest_index = left_child + else + smallest_index = right_child + end + + if @store[index].key > @store[smallest_index].key + swap(index, smallest_index) + end + + return heap_down(smallest_index) 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]