From d1d190052633f7fa7c8aaa8c8fc73df936bff74f Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Thu, 15 Oct 2020 14:00:20 -0700 Subject: [PATCH] added in heap sort and min heap --- lib/heap_sort.rb | 15 +++++++++----- lib/min_heap.rb | 51 ++++++++++++++++++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index 6b692e4..dac5c5f 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,13 @@ - - # This method uses a heap to sort an array. -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(log n) +# Space Complexity: O(n) def heapsort(list) - raise NotImplementedError, "Method not implemented yet..." + heap = MinHeap.new + list.each do |element| + heap.add(element) + end + list.each_with_index do |val, index| + list[index] = heap.remove() + end + return list end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..6212981 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) + # Space Complexity: O(n) 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 # maintaining the heap structure - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(log n) + # Space Complexity: O(n) def remove() - raise NotImplementedError, "Method not implemented yet..." + swap(0, @store.length - 1) + result = @store.pop() + heap_down(0) + + return result.value end @@ -44,10 +49,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 true if @store.length == 0 end private @@ -55,17 +60,37 @@ 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) + # Space complexity: O(n) def heap_up(index) - + return nil if @store.empty? + + predecessor_index = (index - 1)/2 + + if @store[index].key >= @store[predecessor_index].key || index == 0 + return @store + else + swap(index, predecessor_index) + heap_up(predecessor_index) + 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..." + min = index + left, right = 2 * index + 1, 2 * index + 2 + + left < @store.length && @store[left].key < @store[min].key ? min = left : nil + + right < @store.length && @store[right].key < @store[min].key ? min = right : nil + + if min != index + swap(index, min) + heap_down(min) + end end # If you want a swap method... you're welcome