diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..308d5fc 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,61 @@ +require "pry" +# This method uses a heap to sort an array. +# Time Complexity: O (n log n) where n is the number of elements in the heap +# Space Complexity: O (1) +# Adapted from diagrams and pseudocode in https://medium.com/basecs/heapify-all-the-things-with-heap-sort-55ee1c93af82 -# This method uses a heap to sort an array. -# Time Complexity: ? -# Space Complexity: ? -def heap_sort(list) - raise NotImplementedError, "Method not implemented yet..." +def heapsort(list) + limit = list.length + build_max_heap(list) + + while limit > 0 # O(n) + swap(list, 0, limit - 1) + limit -= 1 + heapify(list, 0, limit) # O(log n) + end + + return list +end + +def build_max_heap(list) + index = list.length/2 - 1 + while index >= 0 + heapify(list, index, list.length) + index -= 1 + end +end + + +def heapify(list, index, limit) + root = index + + while index < limit + left = (2 * index) + 1 + right = (2 * index) + 2 + + if left >= limit && right >= limit + return + elsif right >= limit + max = left + else + max = [left, right].max_by {|x| list[x]} + end + + if list[max] > list[root] + root = max + else + return + end + + swap(list, root, index) + + index = root + end +end + +def swap(list, index_1, index_2) + temp = list[index_1] + list[index_1] = list[index_2] + list[index_2] = temp end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..989663d 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -14,18 +14,23 @@ def initialize end # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O (log n) where n is the number of nodes in the store + # Space Complexity: O(1) def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + new_node = HeapNode.new(key, value) + @store << new_node + heap_up(@store.length - 1) end # This method removes and returns an element from the heap # maintaining the heap structure - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O (log n) where n is the number of nodes in the store + # Space Complexity: O(1) def remove() - raise NotImplementedError, "Method not implemented yet..." + swap(0, @store.length - 1) + removed_node = @store.pop + heap_down(0) + return removed_node.value end @@ -47,7 +52,7 @@ def to_s # Time complexity: ? # Space complexity: ? def empty? - raise NotImplementedError, "Method not implemented yet..." + return @store.empty? end private @@ -55,17 +60,35 @@ 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(log n) where n is the number of nodes in the store + # Space complexity: O(1) def heap_up(index) - + return if index == 0 + parent_index = (index - 1) / 2 + if @store[index].key < @store[parent_index].key + swap(index, parent_index) + heap_up(parent_index) + else + return + end end # This helper method takes an index and - # moves it up the heap if it's smaller - # than it's parent node. + # moves it down the heap + # Time complexity: O (log n) where n is the number of nodes in the store + # Space complexity: O(1) def heap_down(index) - raise NotImplementedError, "Method not implemented yet..." + left = (2 * index) + 1 + right = (2 * index) + 2 + if !@store[left] && !@store[right] + return + elsif !@store[right] + min = left + else + min = [left, right].min_by {|x| @store[x].key} + end + swap(index, min) + heap_down(min) 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 = []