diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..9c83fd0 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,67 @@ +require_relative 'min_heap' + +def swap(index1, index2, list) + list[index1], list[index2] = list[index2], list[index1] +end + +# for max heap +# this is helpful https://www.youtube.com/watch?v=2DmK_H7IdTo +# this is also helpful: https://www.programiz.com/dsa/heap-sort +# time: o(logn) +# space: o(logn) - because of call stack? +def heap_down(list, i, length) + left_child = (i * 2) + 1 + right_child = (i * 2) + 2 + max = i + + if left_child < length && list[left_child] > list[max] + max = left_child + end + + if right_child < length && list[right_child] > list[max] + max = right_child + end + + if i != max + swap(i, max, list) + heap_down(list, max, length) + end + +end # 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(logn) - bc of call stack? +def heapsort(list) + # heap = MinHeap.new() + + # until list.empty? # time: o(n) + # heap.add(list.pop) # time: o(logn) heap add, o(1) list.pop, space: o(n) + # end + + # until heap.empty? # time: ? + # list.push(heap.remove) # time: o(logn) heap remove, o(1) list.push, space: o(n) + # end + + # return list + + end_index = list.length - 1 + while end_index >= 0 # time: o(n) + # heapify - build a max heap + # parent nodes start at length / 2 + parent_index = end_index/2 + while parent_index >= 0 + heap_down(list, parent_index, end_index) # time: o(logn) + parent_index -= 1 + end + + # move largest element to the end + swap(0, end_index, list) + + # don't include sorted portion in further heap down + end_index -= 1 + end + + return list +end diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..ef9d9c3 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -14,18 +14,24 @@ def initialize end # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(logn) + # Space Complexity: O(logn) def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + node = HeapNode.new(key, value) + @store.push(node) + return 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(logn) + # Space Complexity: O(logn) def remove() - raise NotImplementedError, "Method not implemented yet..." + return if @store.empty? + swap(0, -1) + removed = @store.pop + heap_down(0) unless @store.empty? + return removed.value end @@ -44,10 +50,10 @@ 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..." + return @store[0].nil? end private @@ -55,17 +61,41 @@ 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(logn) def heap_up(index) - + return if index == 0 + + parent = (index - 1)/2 + if @store[parent].key > @store[index].key + swap(parent, index) + else + return + end + + return heap_up(parent) 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 + + # get child index + if @store[left_child] && @store[right_child] + child_index = @store[left_child].key < @store[right_child].key ? left_child : right_child + elsif @store[left_child] # no case where just right and no left + child_index = left + else # both are null so return + return + end + + if @store[index].key > @store[child_index].key + swap(index, child_index) + return heap_down(child_index) + end end # If you want a swap method... you're welcome