diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index 6b692e4..99b921d 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: ? +# Time Complexity: O(n log(n)) +# Space Complexity: O(n) def heapsort(list) - raise NotImplementedError, "Method not implemented yet..." + + heap = MinHeap.new + + list.each do |num| + heap.add(num) + end + result = [] + until heap.empty? + result << heap.remove + end + return result + end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..32bbd90 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -14,18 +14,32 @@ def initialize end # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(log(n)) + # Space Complexity: O(1) def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + new_element = HeapNode.new(key, value) + next_index = @store.length + @store[next_index] = new_element + heap_up(next_index) + end # This method removes and returns an element from the heap - # maintaining the heap structure - # Time Complexity: ? - # Space Complexity: ? + # maintaining the heap structure + # Time Complexity: O(log(n)) + # Space Complexity: O(1) def remove() - raise NotImplementedError, "Method not implemented yet..." + return if @store.empty? + + last_index = @store.length - 1 + swap(0, last_index) + + temp = @store[last_index] + @store.delete_at(last_index) + heap_down(0) + + return temp.value + end @@ -39,33 +53,73 @@ 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: O(1) + # Space complexity: O(1) def empty? - raise NotImplementedError, "Method not implemented yet..." + return @store.length == 0 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: ? + # Time complexity: O(log(n)) + # Space complexity: O(1) def heap_up(index) - + return if index == 0 # if index is 0 it means its a parent so no need to perform heap up + + if index % 2 == 0 # if index is even number it means its a right chile as right_child = 2*(i) + 2 <-- and this is always even number + parent_index = (index - 2) / 2 + else + parent_index = (index - 1) / 2 + end + + if @store[index].key < @store[parent_index].key + swap(index, parent_index) + heap_up(parent_index) + else + return + end + end - # This helper method takes an index and + # 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) - raise NotImplementedError, "Method not implemented yet..." + return if @store.empty? + last_index = @store.length - 1 + return if index == last_index # if index is the last one, we have reached the right most node + + right_child_idx = (2 * index) + 2 + left_child_idx = (2 * index) + 1 + + return if left_child_idx > last_index # if we reach the end of/edge of the tree return + + # only swap when current parent is greater than either of the child nodes then find the smallest of the two dscendants + # only check the reight child if the right_child_index is less than the max valid index of the array. + if (right_child_idx <= last_index && @store[index].key > @store[right_child_idx].key) || @store[index].key > @store[left_child_idx].key + if (right_child_idx <= last_index && @store[right_child_idx].key < @store[left_child_idx].key) + swap(right_child_idx, index) # swap only when value is greater than child + heap_down(right_child_idx) + else + swap(left_child_idx, index) # swap only when value is greater than child + heap_down(left_child_idx) + end + else + return + 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..fb7fa84 100644 --- a/test/heapsort_test.rb +++ b/test/heapsort_test.rb @@ -1,8 +1,8 @@ require_relative "test_helper" -xdescribe "heapsort" do +describe "heapsort" do it "sorts an empty array" do - # Arrange + # Arrange list = [] # Act @@ -13,7 +13,7 @@ end it "can sort a 1-element array" do - # Arrange + # Arrange list = [5] # Act @@ -22,9 +22,9 @@ # Assert expect(result).must_equal [5] end - + it "can sort a 5-element array" do - # Arrange + # Arrange list = [5, 27, 3, 16, -50] # Act @@ -32,5 +32,5 @@ # Assert expect(result).must_equal [-50, 3, 5, 16, 27] - end + end end \ No newline at end of file