From 104d09758dc90b576a19722f71a91220691c6e3a Mon Sep 17 00:00:00 2001 From: qqdipps Date: Sun, 3 Nov 2019 13:24:53 -0800 Subject: [PATCH 1/4] implements basic add /remove/ empty? operation of min heap --- lib/min_heap.rb | 63 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..27c6bf3 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -14,10 +14,11 @@ def initialize end # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(log(n)) + # Space Complexity: 0(1) 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 @@ -25,7 +26,10 @@ def add(key, value = key) # Time Complexity: ? # Space Complexity: ? def remove() - raise NotImplementedError, "Method not implemented yet..." + removed = @store[0] + @store[0] = @store.pop + heap_down(0) + return removed.value end @@ -55,17 +59,60 @@ 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(1) def heap_up(index) - + parent_i = parent_index(index) + until index == 0 || @store[parent_i].key < @store[index].key + swap(parent_i, index) + index = parent_i + parent_i = parent_index(index) + end + end + + def parent_index(index) + return (index - 1) / 2 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..." + right_child_i = right_child_index(index) + left_child_i = left_child_index(index) + until heaped_down_valid(left_child_i, right_child_i, index) + pos_to_swap = index_of_smallest_child_key(left_child_i, right_child_i) + swap(pos_to_swap, index) + index = pos_to_swap + right_child_i = right_child_index(index) + left_child_i = left_child_index(index) + end + end + + def heaped_down_valid(left_child_i, right_child_i, index) + return !left_child_i || @store[left_child_i].key > @store[index].key && (right_child_i && @store[right_child_i].key > @store[index].key) + end + + def index_of_smallest_child_key(index1, index2) + if !index2 || @store[index2].key > @store[index1].key + return index1 + else + return index2 + end + end + + def right_child_index(index) + if index * 2 + 2 <= @store.length - 1 + return index * 2 + 2 + end + return + end + + def left_child_index(index) + if index * 2 + 1 <= @store.length - 1 + return index * 2 + 1 + end + return end # If you want a swap method... you're welcome From f1699200d64687cc6614f7d9cec6703ea301ee5f Mon Sep 17 00:00:00 2001 From: qqdipps Date: Sun, 3 Nov 2019 17:02:58 -0800 Subject: [PATCH 2/4] implements heapsort of unsorted array inplace --- lib/heap_sort.rb | 98 +++++++++++++++++++++++++++++++++++++++++-- lib/min_heap.rb | 12 +++--- test/heapsort_test.rb | 2 +- 3 files changed, 102 insertions(+), 10 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..dbd1252 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,98 @@ # 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) + heapify(list, parent(list.length - 1)) if list.length > 1 + sort_heaped_list(list) + return list +end + +private +# Time Complexity: O(nlog(n)) +# Space Complexity: O(1) +def sort_heaped_list(list) + last_pos = list.length - 1 + while last_pos > 0 + remove_max(list, last_pos) + last_pos -= 1 + end +end + +# Time Complexity: O(log(n)) +# Space Complexity: O(1) +def remove_max(list, last_pos) + removed = list[0] + list[0] = list[last_pos] + list[last_pos] = removed + heap_down(0, last_pos - 1, list) +end + +# Time Complexity: O(log(n)) +# Space Complexity: O(1) +def heap_down(sub_root, last_pos, list) + max_child = max_child(right_child(sub_root, last_pos), left_child(sub_root, last_pos), list) + while max_child && max_child > sub_root + swap(sub_root, max_child, list) + sub_root = max_child + max_child = max_child(right_child(sub_root, last_pos), left_child(sub_root, last_pos), list) + end +end + +# Time Complexity: O(n) => this is based of the math proof on geeks for geeks, not sure if I 100 % understand ... yet +# Space Complexity: O(n) <= can optimize by using iterative approach over recursion +def heapify(list, sub_root, counter = 0) + max_child = max_child(right_child(sub_root, list.length), left_child(sub_root, list.length), list) + if max_child && list[max_child] > list[sub_root] + swap(max_child, sub_root, list) + heapify(list, max_child, counter + 1) + elsif max_child + heapify(list, sub_root - 1, counter + 1) + end + return +end + + +# Time Complexity: O(1) +# Space Complexity: O(1) +def swap(index1, index2, list) + temp = list[index1] + list[index1] = list[index2] + list[index2] = temp +end + +# Time Complexity: O(1) +# Space Complexity: O(1) +def max_child(left, right, list) + if !right || !left || list[left] > list[right] + return left + end + return right +end + +# Time Complexity: O(1) +# Space Complexity: O(1) +def parent(child) + return (child - 1) / 2 +end + +# Time Complexity: O(1) +# Space Complexity: O(1) +def right_child(root, length) + child = root * 2 + 2 + if child < length + return child + end + return +end + +# Time Complexity: O(1) +# Space Complexity: O(1) +def left_child(root, length) + child = root * 2 + 1 + if child < length + return child + end + return end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 27c6bf3..74b81c9 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -23,8 +23,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(1) def remove() removed = @store[0] @store[0] = @store.pop @@ -48,10 +48,10 @@ def to_s end # This method returns true if the heap is empty - # Time complexity: ? - # Space complexity: ? + # Time complexity: O(1) assuming ruby implements array with memotized length ie attribute of array class rather than method + # Space complexity: O(1) def empty? - raise NotImplementedError, "Method not implemented yet..." + return @store.empty? end private @@ -77,6 +77,8 @@ def parent_index(index) # This helper method takes an index and # moves it up the heap if it's smaller # than it's parent node. + # Time complexity: O(log(n)) + # Space complexity: O(1) def heap_down(index) right_child_i = right_child_index(index) left_child_i = left_child_index(index) 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 9d4d1709203d1cd48c5731d628fef45470793cdb Mon Sep 17 00:00:00 2001 From: qqdipps Date: Sun, 3 Nov 2019 17:02:58 -0800 Subject: [PATCH 3/4] implements heapsort of unsorted array inplace --- lib/heap_sort.rb | 98 +++++++++++++++++++++++++++++++++++++++++-- lib/min_heap.rb | 12 +++--- test/heapsort_test.rb | 2 +- 3 files changed, 102 insertions(+), 10 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..d0ecb6e 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,98 @@ # 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) + heapify(list, parent(list.length - 1)) if list.length > 1 + sort_heaped_list(list) + return list +end + +private +# Time Complexity: O(nlog(n)) +# Space Complexity: O(1) +def sort_heaped_list(list) + last_pos = list.length - 1 + while last_pos > 0 + remove_max(list, last_pos) + last_pos -= 1 + end +end + +# Time Complexity: O(log(n)) +# Space Complexity: O(1) +def remove_max(list, last_pos) + removed = list[0] + list[0] = list[last_pos] + list[last_pos] = removed + heap_down(0, last_pos - 1, list) +end + +# Time Complexity: O(log(n)) +# Space Complexity: O(1) +def heap_down(sub_root, last_pos, list) + max_child = max_child(right_child(sub_root, last_pos), left_child(sub_root, last_pos), list) + while max_child && list[max_child] > list[sub_root] + swap(sub_root, max_child, list) + sub_root = max_child + max_child = max_child(right_child(sub_root, last_pos), left_child(sub_root, last_pos), list) + end +end + +# Time Complexity: O(n) => this is based of the math proof on geeks for geeks, not sure if I 100 % understand ... yet +# Space Complexity: O(n) <= can optimize by using iterative approach over recursion +def heapify(list, sub_root, counter = 0) + max_child = max_child(right_child(sub_root, list.length), left_child(sub_root, list.length), list) + if max_child && list[max_child] > list[sub_root] + swap(max_child, sub_root, list) + heapify(list, max_child, counter + 1) + elsif max_child + heapify(list, sub_root - 1, counter + 1) + end + return +end + + +# Time Complexity: O(1) +# Space Complexity: O(1) +def swap(index1, index2, list) + temp = list[index1] + list[index1] = list[index2] + list[index2] = temp +end + +# Time Complexity: O(1) +# Space Complexity: O(1) +def max_child(left, right, list) + if !right || !left || list[left] > list[right] + return left + end + return right +end + +# Time Complexity: O(1) +# Space Complexity: O(1) +def parent(child) + return (child - 1) / 2 +end + +# Time Complexity: O(1) +# Space Complexity: O(1) +def right_child(root, length) + child = root * 2 + 2 + if child < length + return child + end + return +end + +# Time Complexity: O(1) +# Space Complexity: O(1) +def left_child(root, length) + child = root * 2 + 1 + if child < length + return child + end + return end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 27c6bf3..74b81c9 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -23,8 +23,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(1) def remove() removed = @store[0] @store[0] = @store.pop @@ -48,10 +48,10 @@ def to_s end # This method returns true if the heap is empty - # Time complexity: ? - # Space complexity: ? + # Time complexity: O(1) assuming ruby implements array with memotized length ie attribute of array class rather than method + # Space complexity: O(1) def empty? - raise NotImplementedError, "Method not implemented yet..." + return @store.empty? end private @@ -77,6 +77,8 @@ def parent_index(index) # This helper method takes an index and # moves it up the heap if it's smaller # than it's parent node. + # Time complexity: O(log(n)) + # Space complexity: O(1) def heap_down(index) right_child_i = right_child_index(index) left_child_i = left_child_index(index) 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 fc34fb556170d378ddb4557a4b100ad25a98b57b Mon Sep 17 00:00:00 2001 From: Sav <41908878+qqdipps@users.noreply.github.com> Date: Tue, 5 Nov 2019 16:04:54 -0800 Subject: [PATCH 4/4] merge conflict removed --- lib/heap_sort.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index 6fe420e..779c289 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -33,11 +33,7 @@ def remove_max(list, last_pos) # Space Complexity: O(1) def heap_down(sub_root, last_pos, list) max_child = max_child(right_child(sub_root, last_pos), left_child(sub_root, last_pos), list) -<<<<<<< HEAD while max_child && list[max_child] > list[sub_root] -======= - while max_child && max_child > sub_root ->>>>>>> f1699200d64687cc6614f7d9cec6703ea301ee5f swap(sub_root, max_child, list) sub_root = max_child max_child = max_child(right_child(sub_root, last_pos), left_child(sub_root, last_pos), list) @@ -99,4 +95,4 @@ def left_child(root, length) return child end return -end \ No newline at end of file +end