From 43db88c69ba5cdabe691351ef16aa0b9dcc3df71 Mon Sep 17 00:00:00 2001 From: dnguye2 Date: Tue, 29 Dec 2020 22:25:16 -0800 Subject: [PATCH 1/3] all methods complete and unit tests pass --- lib/min_heap.rb | 68 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 13 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..aac1bed 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -14,18 +14,36 @@ def initialize end # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(logn), worst case we have to swap through each level of a balanced tree + # meaning the height of the tree which is logn where n is the number + # of nodes in our tree + # Space Complexity: O(logn), due to the recursive calls through the heap_up helper method def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + # insert new node to the bottom of the heap + # by placing it at the end of the array + @store.push(HeapNode.new(key, value)) + + if @store.length > 1 + heap_up(@store.length - 1) + end end # This method removes and returns an element from the heap # maintaining the heap structure - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(logn) since worst case we have to go through each level, + # n being number of nodes in the heap + # Space Complexity: O(1) since not using recursion def remove() - raise NotImplementedError, "Method not implemented yet..." + # swap last item in heap with smallest value + swap(0, -1) + + # remove smallest value + removedNode = @store.pop + + # maintain heap structure + heap_down() if @store.length > 1 + + return removedNode.value end @@ -44,10 +62,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 +73,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), n is halved with each recursive call def heap_up(index) + # base case to exit recursion + return if index == 0 + parentIndex = (index - 1) / 2 + + if @store[parentIndex].key > @store[index].key + swap(index, parentIndex) + heap_up(parentIndex) + 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..." + def heap_down(index=0) + return if @store.empty? + + while ((2 * index) + 2) < @store.length # so long as we have a left child, continue to swap + rightIndex = (2 * index) + 1 + leftIndex = (2 * index) + 2 + smallerIndex = leftIndex + if (rightIndex < @store.length) && (@store[rightIndex].key < @store[leftIndex].key) + smallerIndex = rightIndex + end + + if @store[index].key < @store[smallerIndex].key + break + else + swap(index, smallerIndex) + end + index = smallerIndex + end end # If you want a swap method... you're welcome From 89fde1c2bd57a9940dcd8020e72091041dd8c0b5 Mon Sep 17 00:00:00 2001 From: dnguye2 Date: Tue, 29 Dec 2020 23:27:57 -0800 Subject: [PATCH 2/3] fixed references to left and right indices --- lib/min_heap.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index aac1bed..70bc434 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -23,9 +23,7 @@ def add(key, value = key) # by placing it at the end of the array @store.push(HeapNode.new(key, value)) - if @store.length > 1 - heap_up(@store.length - 1) - end + heap_up(@store.length - 1) if @store.length > 1 end # This method removes and returns an element from the heap @@ -34,6 +32,8 @@ def add(key, value = key) # n being number of nodes in the heap # Space Complexity: O(1) since not using recursion def remove() + return if @store.empty? + # swap last item in heap with smallest value swap(0, -1) @@ -93,9 +93,9 @@ def heap_up(index) def heap_down(index=0) return if @store.empty? - while ((2 * index) + 2) < @store.length # so long as we have a left child, continue to swap - rightIndex = (2 * index) + 1 - leftIndex = (2 * index) + 2 + while ((2 * index) + 1) < @store.length # so long as we have a left child, continue to swap + rightIndex = (2 * index) + 2 + leftIndex = (2 * index) + 1 smallerIndex = leftIndex if (rightIndex < @store.length) && (@store[rightIndex].key < @store[leftIndex].key) smallerIndex = rightIndex @@ -110,6 +110,7 @@ def heap_down(index=0) end end + # If you want a swap method... you're welcome def swap(index_1, index_2) temp = @store[index_1] From 601f0c91a15ff88651553bb2fac22dd1c63b3175 Mon Sep 17 00:00:00 2001 From: dnguye2 Date: Tue, 29 Dec 2020 23:28:21 -0800 Subject: [PATCH 3/3] completed heap sort method and pass tests --- lib/heap_sort.rb | 26 +++++++++++++++++++++----- test/heapsort_test.rb | 2 +- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..7fb4cb2 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,24 @@ - +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: O(nlogn) +# Space Complexity: O(n) +def heapsort(list) + return list if list.length <= 1 + + # create heap + heap = MinHeap.new + list.each do |element| + heap.add(element, element) + end + + # sort list by pulling off item from heap + # and replacing current list indices + i = 0 + while i < list.length + list[i] = heap.remove + i += 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 = []