From 8c4ad0385d37a16f24b9a6cdcf6893e7b657987b Mon Sep 17 00:00:00 2001 From: Julia Bouvier Date: Sun, 15 Mar 2020 18:16:35 -0700 Subject: [PATCH 1/2] Implemented min_heap --- lib/min_heap.rb | 55 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..7210e9d 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: log(n) + # Space Complexity: log(n) def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + @store << HeapNode.new(key, value) + 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: log(n) + # Space Complexity: log(n) def remove() - raise NotImplementedError, "Method not implemented yet..." + return nil if @store.empty? + swap(0, @store.length - 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(n) + # Space complexity: O(1) def empty? - raise NotImplementedError, "Method not implemented yet..." + return true if @store.length == 0 end private @@ -55,17 +61,40 @@ 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: log(n) + # Space complexity: log(n) def heap_up(index) - + parent_index = (index - 1) / 2 + return nil if @store.empty? + return if index == 0 || @store[index].key >= @store[parent_index].key + + swap(index, parent_index) + return heap_up(parent_index) 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..." + lc_index = index * 2 + 1 + rc_index = index * 2 + 2 + return if @store[lc_index].nil? && @store[rc_index].nil? + return if index == @store.length - 1 + + if @store[rc_index].nil? && @store[lc_index].key <= @store[index].key + return swap(index, lc_index) + elsif @store[rc_index].nil? && @store[lc_index].key > @store[index].key + return + end + + return unless @store[lc_index].key <= @store[index].key && @store[rc_index].key <= @store[index].key + if @store[lc_index].key <= @store[rc_index].key + swap(index, lc_index) + return heap_down(lc_index) + else + swap(index, rc_index) + return heap_down(rc_index) + end end # If you want a swap method... you're welcome From b5084b8d28c1248ad92f46e935225d63c8383634 Mon Sep 17 00:00:00 2001 From: Julia Bouvier Date: Sun, 15 Mar 2020 18:39:21 -0700 Subject: [PATCH 2/2] Implemented heap_sort --- lib/heap_sort.rb | 20 ++++++++++++++++---- test/heapsort_test.rb | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..9eec07d 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,20 @@ +require_relative "./min_heap" # This method uses a heap to sort an array. -# Time Complexity: ? -# Space Complexity: ? -def heap_sort(list) - raise NotImplementedError, "Method not implemented yet..." +# Time Complexity: n(log(n)) +# Space Complexity: O(1) +def heapsort(list) + heap = MinHeap.new + + until list.empty? + heap.add(list.pop) + end + + index = 0 + until heap.empty? + list[index] = heap.remove() + index += 1 + end + return list end \ No newline at end of file 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 = []