diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..75732a0 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,18 @@ - # This method uses a heap to sort an array. -# Time Complexity: ? -# Space Complexity: ? -def heap_sort(list) - raise NotImplementedError, "Method not implemented yet..." -end \ No newline at end of file +# Time Complexity: O(nlogn) +# Space Complexity: O(n) +def heapsort(list) + return list if list.empty? + heap = MinHeap.new + + list.each do |n| + heap.add(n) + end + + list.length.times do |i| + list[i] = heap.remove + end + + return list +end diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..4151604 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -8,24 +8,33 @@ def initialize(key, value) end class MinHeap + attr_reader :store def initialize @store = [] end # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(logn) + # Space Complexity: O(1) def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + new_i = @store.length + @store[new_i] = HeapNode.new(key, value) + heap_up(new_i) + return key end # This method removes and returns an element from the heap # maintaining the heap structure - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(logn) + # Space Complexity: O(1) def remove() - raise NotImplementedError, "Method not implemented yet..." + value = @store[0].value + + swap(0, -1) + @store.delete_at(-1) + heap_down(0) + return value end @@ -44,10 +53,10 @@ def to_s end # This method returns true if the heap is empty - # Time complexity: ? - # Space complexity: ? + # Time complexity: O(0) + # Space complexity: O(0) def empty? - raise NotImplementedError, "Method not implemented yet..." + return @store.empty? end private @@ -55,17 +64,28 @@ 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(0) def heap_up(index) - + while index > 0 && @store[index].key < @store[(index - 1)/2].key + swap(index, (index - 1)/2) + index = (index - 1)/2 + return if index == 0 + 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..." + if @store[index * 2 + 1] != nil + while @store[index].key > @store[index * 2 + 1].key + swap(index, index * 2 + 1) + swap(index, index * 2 + 2) if @store[index * 2 + 2] != nil && @store[index].key > @store[index * 2 + 2].key + index = index * 2 + 1 + return if @store[index * 2 + 1].nil? + end + 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..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 = []