From 009400c3b6e61a3a3802eddbd90896f32d0515d9 Mon Sep 17 00:00:00 2001 From: Ngoc Le Date: Thu, 3 Oct 2019 00:38:36 -0700 Subject: [PATCH 1/3] implemented add function for min heap --- lib/min_heap.rb | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..87e7a87 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -14,10 +14,12 @@ def initialize end # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(logn) adding element to end of array is O(1) while heap-up worst case is O(logn) + # Space Complexity: O(1) def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + new_node = HeapNode.new(key,value) + @store.push(new_node) + heap_up(@store.length - 1) end # This method removes and returns an element from the heap @@ -25,7 +27,13 @@ def add(key, value = key) # Time Complexity: ? # Space Complexity: ? def remove() - raise NotImplementedError, "Method not implemented yet..." + if @store.empty? + return nil + end + swap(0,@store.length - 1) + result = @store.pop + heap_down(0) + return result.value end @@ -47,7 +55,7 @@ def to_s # Time complexity: ? # Space complexity: ? def empty? - raise NotImplementedError, "Method not implemented yet..." + return (@store.length == 0) ? true : false end private @@ -59,15 +67,34 @@ def empty? # Space complexity: ? def heap_up(index) + if index.odd? + parent_index = (index-1)/2 + else + parent_index = (index-2)/2 + end + while @store[index].key < @store[parent_index].key && parent_index > -1 + swap(index,parent_index) + index = parent_index + if index.odd? + parent_index = (index-1)/2 + else + parent_index = (index-2)/2 + end + 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..." + child_index = 1 + while @store[child_index] != nil && @store[index].key > @store[child_index].key + swap(index, child_index) + index = child_index + child_index = 2 * index + 1 + end + p @store end - # If you want a swap method... you're welcome def swap(index_1, index_2) temp = @store[index_1] From 53c524c99af920cf1cee0d5e7f60e3b652689fa7 Mon Sep 17 00:00:00 2001 From: Ngoc Le Date: Thu, 3 Oct 2019 16:05:26 -0700 Subject: [PATCH 2/3] added remove function --- lib/min_heap.rb | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 87e7a87..62b2f06 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -87,7 +87,36 @@ def heap_up(index) # moves it up the heap if it's smaller # than it's parent node. def heap_down(index) - child_index = 1 + while index < @store.length + left_child_index = 2 * index + 1 + right_child_index = 2 * index + 2 + if !@store.length =< 1 + return + end + # If both childs exist, compare their key find min and compare to parent, swap if parent is smaller + if @store[left_child_index].key && @store[right_child_index].key + if @store[left_child_index].key < @store[right_child_index].key + min_child_index = left_child_index + elsif @store[right_child_index].key < @store[left_child_index].key + min_child_index = right_child_index + end + if @store[index].key > @store[min_child_index].key + swap(index, min_child_index) + index = min_child_index + else + return + end + end + if !@store[right_child_index].key + if @store[index].key > @store[left_child_index].key + swap(index, left_child_index) + index = left_child_index + end + end + end + + # Get the index of both left child and right child + # find the min of the two and get that index then compare to parent swap if parent is greater than child while @store[child_index] != nil && @store[index].key > @store[child_index].key swap(index, child_index) index = child_index From 82a185a27e4f0774868d0b7d65a28978d11647d3 Mon Sep 17 00:00:00 2001 From: Ngoc Le Date: Sat, 5 Oct 2019 15:29:49 -0700 Subject: [PATCH 3/3] fixed remove method and implement heapsort method --- lib/heap_sort.rb | 15 ++++++++-- lib/min_heap.rb | 70 +++++++++++++++++-------------------------- test/heapsort_test.rb | 2 +- 3 files changed, 41 insertions(+), 46 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..c4f3b37 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,17 @@ - +require 'min_heap.rb' # 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) + return [] if list.empty? + min_heap = MinHeap.new + list.each do |num| + min_heap.add(num) + end + sorted_array = [] + while !min_heap.empty? + sorted_array << min_heap.remove() + end + return sorted_array end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 62b2f06..5c8f2d9 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -24,12 +24,9 @@ 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(logn) + # Space Complexity: O(1) def remove() - if @store.empty? - return nil - end swap(0,@store.length - 1) result = @store.pop heap_down(0) @@ -52,8 +49,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? return (@store.length == 0) ? true : false end @@ -63,8 +60,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(logn) + # Space complexity: O(1) def heap_up(index) if index.odd? @@ -87,47 +84,36 @@ def heap_up(index) # moves it up the heap if it's smaller # than it's parent node. def heap_down(index) - while index < @store.length - left_child_index = 2 * index + 1 - right_child_index = 2 * index + 2 - if !@store.length =< 1 - return - end - # If both childs exist, compare their key find min and compare to parent, swap if parent is smaller - if @store[left_child_index].key && @store[right_child_index].key - if @store[left_child_index].key < @store[right_child_index].key - min_child_index = left_child_index - elsif @store[right_child_index].key < @store[left_child_index].key - min_child_index = right_child_index - end - if @store[index].key > @store[min_child_index].key - swap(index, min_child_index) - index = min_child_index - else - return - end - end - if !@store[right_child_index].key - if @store[index].key > @store[left_child_index].key - swap(index, left_child_index) - index = left_child_index - end + + left_child_index = 2 * index + 1 + right_child_index = 2 * index + 2 + + if @store[left_child_index].nil? + return nil + end + if @store[right_child_index].nil? + if @store[index].key > @store[left_child_index].key + swap(index, left_child_index) end + return end - - # Get the index of both left child and right child - # find the min of the two and get that index then compare to parent swap if parent is greater than child - while @store[child_index] != nil && @store[index].key > @store[child_index].key - swap(index, child_index) - index = child_index - child_index = 2 * index + 1 + # If both child exist, compare their key find min and compare to parent, swap if parent is smaller + + if @store[left_child_index].key < @store[right_child_index].key + swap(left_child_index, index) + heap_down(left_child_index) + else + swap(right_child_index, index) + heap_down(left_child_index) end - p @store + 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 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 = []