From f5252f7ac3cf642c0cc8dc78905f07439c1c368a Mon Sep 17 00:00:00 2001 From: Yinjing Lin Date: Wed, 5 May 2021 23:34:26 -0700 Subject: [PATCH 1/3] heap_sort not complete --- lib/heap_sort.rb | 13 +++++++---- lib/min_heap.rb | 51 ++++++++++++++++++++++++++++++++----------- test/heapsort_test.rb | 2 +- 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..fcdc122 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: ? -def heap_sort(list) - raise NotImplementedError, "Method not implemented yet..." -end \ No newline at end of file +def heapsort(list) + return list if list.empty? + heap = MinHeap.new + + list.each do |n| + heap.add(n) + end + return heap.store.map {|node| node.key} +end diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..972a5ec 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -8,24 +8,39 @@ def initialize(key, value) end class MinHeap + attr_reader :store def initialize @store = [] end # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(logn) + # Space Complexity: O(1) def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + if @store.empty? + @store << HeapNode.new(key, value) + else + new_i = @store.length + @store[new_i] = HeapNode.new(key, value) + heap_up(new_i) + end + return key end # This method removes and returns an element from the heap # maintaining the heap structure - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(logn) + # Space Complexity: O(1) def remove() - raise NotImplementedError, "Method not implemented yet..." + min = @store.min_by {|node| node.key} + index = @store.find_index {|node| node.key == min.key} + value = @store[index].value + + swap(index, -1) + @store.delete_at(-1) + heap_down(index) + return value end @@ -44,10 +59,10 @@ def to_s end # This method returns true if the heap is empty - # Time complexity: ? - # Space complexity: ? + # Time complexity: O(0) + # Space complexity: O(0) def empty? - raise NotImplementedError, "Method not implemented yet..." + return @store.empty? end private @@ -55,17 +70,27 @@ 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(0) def heap_up(index) - + while @store[index].key < @store[(index - 1)/2].key + swap(index, (index - 1)/2) + index = (index - 1)/2 + return if index == 0 + 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..." + if @store[index * 2 + 1] != nil + while @store[index].key > @store[index * 2 + 1].key + swap(index, index * 2 + 1) + index = index * 2 + 1 + return if @store[index * 2 + 1].nil? + end + 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 6611cd713bdfec744514e621a50c66fe1fa665e1 Mon Sep 17 00:00:00 2001 From: Yinjing Lin Date: Thu, 6 May 2021 12:04:18 -0700 Subject: [PATCH 2/3] fix some bugs --- lib/min_heap.rb | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 972a5ec..d07703d 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -18,13 +18,9 @@ def initialize # Time Complexity: O(logn) # Space Complexity: O(1) def add(key, value = key) - if @store.empty? - @store << HeapNode.new(key, value) - else - new_i = @store.length - @store[new_i] = HeapNode.new(key, value) - heap_up(new_i) - end + new_i = @store.length + @store[new_i] = HeapNode.new(key, value) + heap_up(new_i) return key end @@ -33,13 +29,11 @@ def add(key, value = key) # Time Complexity: O(logn) # Space Complexity: O(1) def remove() - min = @store.min_by {|node| node.key} - index = @store.find_index {|node| node.key == min.key} - value = @store[index].value + value = @store[0].value - swap(index, -1) + swap(0, -1) @store.delete_at(-1) - heap_down(index) + heap_down(0) return value end @@ -73,7 +67,7 @@ def empty? # Time complexity: O(logn) # Space complexity: O(0) def heap_up(index) - while @store[index].key < @store[(index - 1)/2].key + while index > 0 && @store[index].key < @store[(index - 1)/2].key swap(index, (index - 1)/2) index = (index - 1)/2 return if index == 0 @@ -87,6 +81,8 @@ def heap_down(index) if @store[index * 2 + 1] != nil while @store[index].key > @store[index * 2 + 1].key swap(index, index * 2 + 1) + swap(index, index * 2 + 2) if @store[index * 2 + 2] != nil && @store[index].key > @store[index * 2 + 2].key + swap(index * 2 + 1, index * 2 + 2) if @store[index * 2 + 2] != nil && @store[index * 2 + 1].key > @store[index * 2 + 2].key index = index * 2 + 1 return if @store[index * 2 + 1].nil? end From ca22fffc311b1351d9b79fa39bf756392c655d1b Mon Sep 17 00:00:00 2001 From: Yinjing Lin Date: Thu, 6 May 2021 14:36:08 -0700 Subject: [PATCH 3/3] pass all tests --- lib/heap_sort.rb | 11 ++++++++--- lib/min_heap.rb | 1 - 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index fcdc122..75732a0 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,7 +1,7 @@ # This method uses a heap to sort an array. -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(nlogn) +# Space Complexity: O(n) def heapsort(list) return list if list.empty? heap = MinHeap.new @@ -9,5 +9,10 @@ def heapsort(list) list.each do |n| heap.add(n) end - return heap.store.map {|node| node.key} + + list.length.times do |i| + list[i] = heap.remove + end + + return list end diff --git a/lib/min_heap.rb b/lib/min_heap.rb index d07703d..4151604 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -82,7 +82,6 @@ def heap_down(index) while @store[index].key > @store[index * 2 + 1].key swap(index, index * 2 + 1) swap(index, index * 2 + 2) if @store[index * 2 + 2] != nil && @store[index].key > @store[index * 2 + 2].key - swap(index * 2 + 1, index * 2 + 2) if @store[index * 2 + 2] != nil && @store[index * 2 + 1].key > @store[index * 2 + 2].key index = index * 2 + 1 return if @store[index * 2 + 1].nil? end