From 04f83e6daa9d89dc4be453270cc4dd81faefd680 Mon Sep 17 00:00:00 2001 From: gessica Date: Thu, 29 Apr 2021 14:27:40 -0700 Subject: [PATCH 1/2] implemented methods and tests are passing --- lib/heap_sort.rb | 19 +++++++++++++++-- lib/min_heap.rb | 49 ++++++++++++++++++++++++++++++++++++++----- test/heapsort_test.rb | 2 +- 3 files changed, 62 insertions(+), 8 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..f6793e4 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -3,6 +3,21 @@ # This method uses a heap to sort an array. # Time Complexity: ? # Space Complexity: ? -def heap_sort(list) - raise NotImplementedError, "Method not implemented yet..." +def heapsort(list) + # raise NotImplementedError, "Method not implemented yet..." + + heap = MinHeap.new + + list.each do |element| + heap.add(element) + end + + sorted_list = [] + + until heap.empty? + sorted_list.push(heap.remove) + end + + return sorted_list + end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..dfdba61 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -17,7 +17,10 @@ def initialize # Time Complexity: ? # Space Complexity: ? def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + # 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 @@ -25,7 +28,17 @@ def add(key, value = key) # Time Complexity: ? # Space Complexity: ? def remove() - raise NotImplementedError, "Method not implemented yet..." + # raise NotImplementedError, "Method not implemented yet..." + if @store.empty? + return nil + end + + swap(0, @store.length - 1) + result = @store.pop.value + + heap_down(0) unless @store.empty? + return result + end @@ -47,7 +60,8 @@ def to_s # Time complexity: ? # Space complexity: ? def empty? - raise NotImplementedError, "Method not implemented yet..." + # raise NotImplementedError, "Method not implemented yet..." + return @store.empty? end private @@ -58,14 +72,39 @@ def empty? # Time complexity: ? # Space complexity: ? def heap_up(index) - + return nil if index == 0 + + parent_index = (index - 1)/ 2 + + if @store[index].key < @store[parent_index].key + swap(index, parent_index) + heap_up(parent_index) + else + return nil + 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..." + # raise NotImplementedError, "Method not implemented yet..." + left_child = (index * 2) + 1 + right_child = (index * 2) + 2 + + return @store if @store[left_child].nil? || @store[right_child].nil? + + if @store[left_child].key < @store[index].key + swap(left_child, index) + heap_down(left_child) + end + + if @store[right_child].key < @store[index].key + swap(right_child, index) + heap_down(right_child) + 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..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 = [] From 743383e55609370fba4a8c4c1c15cf91ede0dfae Mon Sep 17 00:00:00 2001 From: gessica Date: Thu, 29 Apr 2021 14:45:19 -0700 Subject: [PATCH 2/2] added time and space complexity to all methods --- lib/heap_sort.rb | 4 ++-- lib/min_heap.rb | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index f6793e4..73eb394 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,8 @@ # This method uses a heap to sort an array. -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(nlog(n)) +# Space Complexity: O(n) def heapsort(list) # raise NotImplementedError, "Method not implemented yet..." diff --git a/lib/min_heap.rb b/lib/min_heap.rb index dfdba61..5ece5de 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: ? - # Space Complexity: ? + # Time Complexity: O(log(n)) + # Space Complexity: O(log(n)) def add(key, value = key) # raise NotImplementedError, "Method not implemented yet..." @store << HeapNode.new(key, value) @@ -25,8 +25,8 @@ def add(key, value = key) # 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(log(n)) def remove() # raise NotImplementedError, "Method not implemented yet..." if @store.empty? @@ -57,8 +57,8 @@ 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? @@ -69,8 +69,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: ? - # Space complexity: ? + # Time complexity: O(log(n)) + # Space complexity: O(log(n)) def heap_up(index) return nil if index == 0