diff --git a/README.md b/README.md index 762c2d5..d39e425 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ The methods you will need to write are: ## HeapSort -The last method to write is `heapsort(list)` which takes an array and uses a `Heap` instance to sort the array. We have discussed this method before. It should look somewhat like insertion sort. +The last method to write is `(list)` which takes an array and uses a `Heap` instance to sort the array. We have discussed this method before. It should look somewhat like insertion sort. Make certain you also document the expected runtime of each method. diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..4c4aea4 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -4,5 +4,22 @@ # Time Complexity: ? # Space Complexity: ? def heap_sort(list) - raise NotImplementedError, "Method not implemented yet..." + if list.empty? + return [] + end + + heap = MinHeap.new + + list.each do |val| + heap.add(val) + end + + sorted = [] + + until heap.empty? + sorted << heap.remove + end + + + return sorted end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..fc4e223 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -17,7 +17,9 @@ def initialize # Time Complexity: ? # Space Complexity: ? 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 @@ -25,7 +27,14 @@ 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) unless @store.empty? + + removed.value end @@ -47,7 +56,7 @@ def to_s # Time complexity: ? # Space complexity: ? def empty? - raise NotImplementedError, "Method not implemented yet..." + @store.empty? end private @@ -58,14 +67,37 @@ def empty? # Time complexity: ? # Space complexity: ? def heap_up(index) - + if index == 0 + return + end + + head = (index - 1 ) / 2 + + if index == 0 + return + elsif @store[index].key < @store[head].key + swap(head,index) + heap_up(head) + 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 = (index * 2) + 1 + right = (index * 2) + 2 + if left >= @store.length - 1 + return + end + if @store[right] && @store[right].key < @store[left].key && @store[right].key < @store[index].key + swap(right, index) + heap_down(right) + elsif @store[left].key < @store[index].key + swap(left, index) + heap_down(left) + 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..76e3fdc 100644 --- a/test/heapsort_test.rb +++ b/test/heapsort_test.rb @@ -1,12 +1,12 @@ require_relative "test_helper" -xdescribe "heapsort" do +describe "heap_sort" 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]