From 93d844e2ebae3515530f3c724bf555836efa7762 Mon Sep 17 00:00:00 2001 From: Kate Sandler Date: Thu, 26 Sep 2019 11:54:26 -0700 Subject: [PATCH 1/7] You're probably not interested in my VSCode Settings. --- .vscode/settings.json | 5 +++ lib/heap_sort.rb | 21 +++++++++--- lib/min_heap.rb | 74 +++++++++++++++++++++++++++---------------- 3 files changed, 67 insertions(+), 33 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..953e321 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "workbench.colorCustomizations": { + "tab.unfocusedActiveBorder": "#fff0" + } +} \ No newline at end of file diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..45b8fe5 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,19 @@ -# This method uses a heap to sort an array. -# Time Complexity: ? -# Space Complexity: ? -def heap_sort(list) - raise NotImplementedError, "Method not implemented yet..." +# This method passes the test. I suspect that means it's using a heap to sort an array. +# Time Complexity: O(nlog(n)) +# Space Complexity: All the good stuff happens in place. +def heapsort(vault) + hoard = MinHeap.new + + hoard.each do |treasure| + vault.add(treasure, treasure) + end + + the_precious = [] + until hoard.empty? + the_precious << treasure.remove + end + + return the_precious end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..0d0438f 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -13,19 +13,24 @@ def initialize @store = [] end - # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: ? TODO: Answer this. + # Space Complexity: ? TODO: Answer this. It's done in place. def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + node = HeapNode.new(key, value) + @store.push(node) + 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: TODO: Answer this. + # Space Complexity: TODO: Answer this. It's done in place. def remove() - raise NotImplementedError, "Method not implemented yet..." + return nil if !@store + + swap(0, @store.length - 1) + nope = @store.pop + + heap_down(0) + return nope.value end @@ -39,39 +44,52 @@ def to_s end output += @store.last.value + "]" - + return output end - # This method returns true if the heap is empty - # Time complexity: ? - # Space complexity: ? + # Time complexity: Exceedingly fast. + # Space complexity: One bit. def empty? - raise NotImplementedError, "Method not implemented yet..." + !@store end private - # 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: TODO: Answer this. + # Space complexity: TODO: Compares all the things one after another. Swapper stores wee variables. + def heap_up(i) + return if i == 0 + + pnode = (i-1) / 2 + if @store[i].key < @store[pnode].key + swap(i, pnode) + heap_up(pnode) + 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(i) + li = i*2+1 + ri = i*2+2 + + if ri < @store.length + + min = @store[li].key < @store[ri].key ? li : ri + + if @store[i].key > @store[min].key + swap(i, min) + heap_down(min) + end + elsif li < @store.length + if @store[i].key > @store[li].key + swap(i, li) + end + end end - # If you want a swap method... you're welcome def swap(index_1, index_2) temp = @store[index_1] @store[index_1] = @store[index_2] @store[index_2] = temp end -end \ No newline at end of file +end From 8e31a45d0d712a88e30f5f5e5a88f809e2d854fe Mon Sep 17 00:00:00 2001 From: Kate Sandler Date: Thu, 26 Sep 2019 12:10:38 -0700 Subject: [PATCH 2/7] Heaps of changes. Simply heaps of them. --- .vscode/settings.json | 5 ----- lib/heap_sort.rb | 5 +---- lib/min_heap.rb | 12 ++++++------ 3 files changed, 7 insertions(+), 15 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 953e321..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "workbench.colorCustomizations": { - "tab.unfocusedActiveBorder": "#fff0" - } -} \ No newline at end of file diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index 45b8fe5..ea3584a 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,5 @@ - - -# This method passes the test. I suspect that means it's using a heap to sort an array. # Time Complexity: O(nlog(n)) -# Space Complexity: All the good stuff happens in place. +# Space Complexity: Constant. def heapsort(vault) hoard = MinHeap.new diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 0d0438f..6c48ad5 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -13,16 +13,16 @@ def initialize @store = [] end - # Time Complexity: ? TODO: Answer this. - # Space Complexity: ? TODO: Answer this. It's done in place. + # Time Complexity: ? O(log n) + # Space Complexity: ? O(n) def add(key, value = key) node = HeapNode.new(key, value) - @store.push(node) + @store << node heap_up(@store.length - 1) end # Time Complexity: TODO: Answer this. - # Space Complexity: TODO: Answer this. It's done in place. + # Space Complexity: O(n) def remove() return nil if !@store @@ -49,7 +49,7 @@ def to_s end # Time complexity: Exceedingly fast. - # Space complexity: One bit. + # Space complexity: Literally one bit. def empty? !@store end @@ -57,7 +57,7 @@ def empty? private # Time complexity: TODO: Answer this. - # Space complexity: TODO: Compares all the things one after another. Swapper stores wee variables. + # Space complexity: O(n) def heap_up(i) return if i == 0 From 17502ab549e023fee35d0e93ebefbb11e1713ef2 Mon Sep 17 00:00:00 2001 From: Kate Sandler Date: Thu, 26 Sep 2019 12:30:24 -0700 Subject: [PATCH 3/7] Added operational complexities. --- .gitignore | 1 + lib/min_heap.rb | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index e43b0f9..4d0e806 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .DS_Store +.vscode/settings.json diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6c48ad5..a286a65 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -21,7 +21,7 @@ def add(key, value = key) heap_up(@store.length - 1) end - # Time Complexity: TODO: Answer this. + # Time Complexity: O(log n) # Space Complexity: O(n) def remove() return nil if !@store @@ -56,7 +56,7 @@ def empty? private - # Time complexity: TODO: Answer this. + # Time complexity: O(log n) # Space complexity: O(n) def heap_up(i) return if i == 0 From 192a5df9fe63e726bc10e846f698ecb45e5cbc16 Mon Sep 17 00:00:00 2001 From: Kate Sandler Date: Thu, 26 Sep 2019 13:51:33 -0700 Subject: [PATCH 4/7] Fixed an error in my brain, then fixed it in my code. --- lib/min_heap.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index a286a65..7010db9 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -14,7 +14,7 @@ def initialize end # Time Complexity: ? O(log n) - # Space Complexity: ? O(n) + # Space Complexity: ? O(nm) def add(key, value = key) node = HeapNode.new(key, value) @store << node @@ -22,7 +22,7 @@ def add(key, value = key) end # Time Complexity: O(log n) - # Space Complexity: O(n) + # Space Complexity: Already have all the space we need. def remove() return nil if !@store @@ -57,7 +57,7 @@ def empty? private # Time complexity: O(log n) - # Space complexity: O(n) + # Space complexity: O(nm) def heap_up(i) return if i == 0 From adee0eeac67360a6aeb5f43433dee0d033088bb7 Mon Sep 17 00:00:00 2001 From: Kate Sandler Date: Thu, 17 Oct 2019 15:19:15 -0700 Subject: [PATCH 5/7] Changes per review. --- lib/heap_sort.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index ea3584a..5ee91c9 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,16 +1,16 @@ # Time Complexity: O(nlog(n)) # Space Complexity: Constant. -def heapsort(vault) - hoard = MinHeap.new +def heapsort(array) + heap = MinHeap.new - hoard.each do |treasure| - vault.add(treasure, treasure) + hoard.each do |item| + vault.add(item, item) end - the_precious = [] - until hoard.empty? - the_precious << treasure.remove + result = [] + until heap.empty? + result << item.remove end - return the_precious + return result end \ No newline at end of file From 2d30140bd83b92c4442a47fc2e41b89c25629148 Mon Sep 17 00:00:00 2001 From: Kate Sandler Date: Thu, 17 Oct 2019 15:20:38 -0700 Subject: [PATCH 6/7] Changes per review. --- lib/heap_sort.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index 5ee91c9..af20725 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -3,8 +3,8 @@ def heapsort(array) heap = MinHeap.new - hoard.each do |item| - vault.add(item, item) + array.each do |item| + heap.add(item, item) end result = [] From beb6227434273fb826e843f700dfccce91e41145 Mon Sep 17 00:00:00 2001 From: Kate Sandler Date: Thu, 17 Oct 2019 15:24:05 -0700 Subject: [PATCH 7/7] Changes per review. --- lib/min_heap.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 7010db9..df1d1a4 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -14,7 +14,7 @@ def initialize end # Time Complexity: ? O(log n) - # Space Complexity: ? O(nm) + # Space Complexity: ? O(log n)) def add(key, value = key) node = HeapNode.new(key, value) @store << node @@ -22,7 +22,7 @@ def add(key, value = key) end # Time Complexity: O(log n) - # Space Complexity: Already have all the space we need. + # Space Complexity: O(log n) def remove() return nil if !@store @@ -48,8 +48,8 @@ def to_s return output end - # Time complexity: Exceedingly fast. - # Space complexity: Literally one bit. + # Time complexity: O(1) + # Space complexity: O(1) def empty? !@store end @@ -57,7 +57,7 @@ def empty? private # Time complexity: O(log n) - # Space complexity: O(nm) + # Space complexity: Log n def heap_up(i) return if i == 0