From c1e5c81a133e76306ea1545fe08f5457b28edf2e Mon Sep 17 00:00:00 2001 From: Iris Lux Date: Wed, 19 May 2021 21:01:11 -0700 Subject: [PATCH 1/2] completed assignment --- lib/heap_sort.rb | 14 +++++++---- lib/min_heap.rb | 55 +++++++++++++++++++++++++++++++------------ test/heapsort_test.rb | 8 +++---- test/min_heap_test.rb | 4 +++- 4 files changed, 57 insertions(+), 24 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..13ef029 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,14 @@ - +require_relative "min_heap" # This method uses a heap to sort an array. -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: o(nlogn) +# Space Complexity: o(nlogn) def heap_sort(list) - raise NotImplementedError, "Method not implemented yet..." + #add elements to a heap + #remove elements from heap one-by one, placing them in order + sort_heap = MinHeap.new + sorted = [] + list.each{|element| sort_heap.add(element)} + list.length.times{|i| sorted.push(sort_heap.remove)} + return sorted end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..b0fdd63 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -14,18 +14,25 @@ def initialize end # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(nlogn) + # Space Complexity: O(nlogn) 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(nlogn) + # Space Complexity: O(nlogn) def remove() - raise NotImplementedError, "Method not implemented yet..." + return nil if @store.empty? + swap(0, @store.length - 1) + result = @store.pop.value + + heap_down(0) unless @store.empty? + return result end @@ -44,10 +51,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.empty? end private @@ -55,17 +62,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: ? - def heap_up(index) - + # Time complexity: O(nlogn) + # Space complexity: O(nlogn) + def heap_up(idx) + parent_idx = (idx - 1) / 2 + return if idx <= 0 || @store[parent_idx].key <= @store[idx].key + swap(parent_idx, idx) + heap_up(parent_idx) 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..." + # Time complexity: O(nlogn) + # Space complexity: O(nlogn) + def heap_down(idx) + + left_idx = (idx * 2) + 1 + right_idx = (idx * 2) + 2 + smaller_idx = idx + + smaller_idx = left_idx if @store[left_idx] && @store[left_idx].key < @store[smaller_idx].key + + smaller_idx = right_idx if @store[right_idx] && @store[right_idx].key < @store[smaller_idx].key + + if (smaller_idx != idx) + swap(idx, smaller_idx) + heap_down(smaller_idx) + end + end # If you want a swap method... you're welcome diff --git a/test/heapsort_test.rb b/test/heapsort_test.rb index 34402ac..76e3fdc 100644 --- a/test/heapsort_test.rb +++ b/test/heapsort_test.rb @@ -1,12 +1,12 @@ require_relative "test_helper" -xdescribe "heapsort" do +describe "heap_sort" do it "sorts an empty array" do # Arrange list = [] # Act - result = heapsort(list) + result = heap_sort(list) # Assert expect(result).must_equal [] @@ -17,7 +17,7 @@ list = [5] # Act - result = heapsort(list) + result = heap_sort(list) # Assert expect(result).must_equal [5] @@ -28,7 +28,7 @@ list = [5, 27, 3, 16, -50] # Act - result = heapsort(list) + result = heap_sort(list) # Assert expect(result).must_equal [-50, 3, 5, 16, 27] diff --git a/test/min_heap_test.rb b/test/min_heap_test.rb index 186d4c2..50a40e1 100644 --- a/test/min_heap_test.rb +++ b/test/min_heap_test.rb @@ -61,11 +61,12 @@ heap.add(57, "Cake") # Act + removed = heap.remove # Assert expect(removed).must_equal "Donuts" - + puts heap # Another Act removed = heap.remove @@ -73,6 +74,7 @@ expect(removed).must_equal "Pizza" # Another Act + puts heap removed = heap.remove # Another assert From 8aefa191feab8039fa997c733890768baa1869dd Mon Sep 17 00:00:00 2001 From: Iris Lux Date: Wed, 19 May 2021 21:03:50 -0700 Subject: [PATCH 2/2] fixed comments --- lib/min_heap.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index b0fdd63..61f943c 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -14,8 +14,8 @@ def initialize end # This method adds a HeapNode instance to the heap - # Time Complexity: O(nlogn) - # Space Complexity: O(nlogn) + # Time Complexity: O(logn) + # Space Complexity: O(logn) def add(key, value = key) @store << HeapNode.new(key, value) @@ -24,8 +24,8 @@ def add(key, value = key) # This method removes and returns an element from the heap # maintaining the heap structure - # Time Complexity: O(nlogn) - # Space Complexity: O(nlogn) + # Time Complexity: O(logn) + # Space Complexity: O(logn) def remove() return nil if @store.empty? swap(0, @store.length - 1) @@ -62,8 +62,8 @@ 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: O(nlogn) - # Space complexity: O(nlogn) + # Time complexity: O(logn) + # Space complexity: O(logn) def heap_up(idx) parent_idx = (idx - 1) / 2 return if idx <= 0 || @store[parent_idx].key <= @store[idx].key @@ -74,8 +74,8 @@ def heap_up(idx) # This helper method takes an index and # moves it up the heap if it's smaller # than it's parent node. - # Time complexity: O(nlogn) - # Space complexity: O(nlogn) + # Time complexity: O(logn) + # Space complexity: O(logn) def heap_down(idx) left_idx = (idx * 2) + 1