diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index 6b692e4..e762cb4 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,25 @@ - +require_relative 'min_heap' +require 'pry' # This method uses a heap to sort an array. -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n log n) +# Space Complexity: O(n) def heapsort(list) - raise NotImplementedError, "Method not implemented yet..." + + return [] if list.empty? + # create heap + heap = MinHeap.new + + list.each do |element| + heap.add(element) + end + + until heap.empty? + current = heap.remove + list.push(current) + list.shift unless list.length == 1 + end + + return list + end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..e2f51e2 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -1,3 +1,4 @@ + class HeapNode attr_reader :key, :value @@ -14,18 +15,28 @@ def initialize end # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + + @store.push(HeapNode.new(key,value)) + child = @store.length - 1 + heap_up(child) if child != 0 + end # This method removes and returns an element from the heap # maintaining the heap structure - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def remove() - raise NotImplementedError, "Method not implemented yet..." + + swap(0, @store.length - 1) + deleted = @store.pop + heap_down(0) unless self.empty? + + return deleted.value + end @@ -44,10 +55,15 @@ 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..." + if @store[0].nil? || @store.empty? + return true + end + + return false + end private @@ -55,17 +71,45 @@ 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(n) + # Space complexity: O(1) def heap_up(index) - + + parent = (index - 2) / 2 if index.even? + parent = (index - 1) / 2 if index.odd? + + if @store[parent].key > @store[index].key + swap(parent, index) + heap_up(parent) if parent != 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..." + + left = (2 * index) + 1 + right = (2 * index) + 2 + + + if !@store[right].nil? + min_child = left if @store[left].key < @store[right].key + min_child = right if @store[left].key > @store[right].key + elsif @store[left].nil? + return + else + min_child = left + end + + if @store[index].key > @store[min_child].key + swap(index, min_child) + index = min_child + heap_down(index) + 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 = [] diff --git a/test/min_heap_test.rb b/test/min_heap_test.rb index 186d4c2..d3193fc 100644 --- a/test/min_heap_test.rb +++ b/test/min_heap_test.rb @@ -59,6 +59,7 @@ heap.add(0, "Donuts") heap.add(16, "Cookies") heap.add(57, "Cake") + # Act removed = heap.remove