From 5456bc0e67a483fe395672d8174c1aa84bebfb3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannah=20Teki=C3=A9?= Date: Mon, 4 Jan 2021 15:47:29 -0800 Subject: [PATCH 1/3] added timeandspace complexity --- Gemfile | 11 ++++---- Rakefile | 4 +-- lib/heap_sort.rb | 21 ++++++++++----- lib/min_heap.rb | 60 +++++++++++++++++++++++++++++++------------ test/heapsort_test.rb | 14 +++++----- test/min_heap_test.rb | 44 +++++++++++++++---------------- test/test_helper.rb | 10 ++++---- 7 files changed, 100 insertions(+), 64 deletions(-) diff --git a/Gemfile b/Gemfile index 04a9dcd..1e2cf3f 100644 --- a/Gemfile +++ b/Gemfile @@ -1,10 +1,9 @@ # frozen_string_literal: true source "https://rubygems.org" -gem 'rake' -gem 'minitest' -gem 'minitest-spec' -gem 'minitest-reporters' +gem "rake" +gem "minitest" +gem "minitest-spec" +gem "minitest-reporters" gem "pry" -gem 'minitest-skip' - +gem "minitest-skip" diff --git a/Rakefile b/Rakefile index 0c2d13f..8aebe3b 100644 --- a/Rakefile +++ b/Rakefile @@ -1,9 +1,9 @@ -require 'rake/testtask' +require "rake/testtask" Rake::TestTask.new do |t| t.libs = ["lib"] t.warning = true - t.test_files = FileList['test/*_test.rb'] + t.test_files = FileList["test/*_test.rb"] end task default: :test diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..40db1bd 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,17 @@ - +require_relative "./min_heap" # 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 +# Time Complexity: O(long n) +# Space Complexity: O(n) +def heapsort(list) + min_heap = MinHeap.new() + list.each do |item| + min_heap.add(item) + end + empty_arr = [] + + list.length.times do + empty_arr << min_heap.remove() + end + return empty_arr +end diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..e511bac 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -8,27 +8,56 @@ def initialize(key, value) end class MinHeap - def initialize @store = [] end # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O (log n) + # Space Complexity: O (n) def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + heap_node = HeapNode.new(key, value) + @store.push(heap_node) + curent_index = @store.length - 1 + parent_index = (curent_index - 1) / 2 + while heap_node.key < @store[parent_index].key + swap(curent_index, parent_index) + curent_index = parent_index + parent_index = (curent_index - 1) / 2 + break if parent_index < 0 + end end # 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 (n) def remove() - raise NotImplementedError, "Method not implemented yet..." + return nil if @store.empty? + swap(0, @store.length - 1) + result = @store.pop + curent_index = 0 + left_child_index = 1 + right_child_index = 2 + while @store[left_child_index] || @store[right_child_index] + if @store[right_child_index] && @store[right_child_index].key < @store[left_child_index].key && @store[right_child_index].key < @store[curent_index].key + swap(curent_index, right_child_index) + curent_index = right_child_index + elsif @store[right_child_index] && @store[right_child_index].key > @store[left_child_index].key && @store[left_child_index].key < @store[curent_index].key + swap(curent_index, left_child_index) + curent_index = left_child_index + elsif @store[left_child_index] && @store[left_child_index].key < @store[curent_index].key + swap(curent_index, left_child_index) + curent_index = left_child_index + else + break + end + left_child_index = (curent_index * 2) + 1 + right_child_index = (curent_index * 2) + 2 + end + return result.value end - # Used for Testing def to_s return "[]" if @store.empty? @@ -39,15 +68,15 @@ 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.empty? end private @@ -58,14 +87,13 @@ def empty? # Time complexity: ? # Space complexity: ? def heap_up(index) - 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. def heap_down(index) - raise NotImplementedError, "Method not implemented yet..." + end # If you want a swap method... you're welcome @@ -74,4 +102,4 @@ def swap(index_1, index_2) @store[index_1] = @store[index_2] @store[index_2] = temp end -end \ No newline at end of file +end diff --git a/test/heapsort_test.rb b/test/heapsort_test.rb index 34402ac..9964e33 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 \ No newline at end of file + end +end diff --git a/test/min_heap_test.rb b/test/min_heap_test.rb index 186d4c2..6b1a05b 100644 --- a/test/min_heap_test.rb +++ b/test/min_heap_test.rb @@ -1,9 +1,9 @@ require_relative "test_helper" describe "Heap" do - let(:heap) {MinHeap.new} + let(:heap) { MinHeap.new } it "can be created" do - + # Assert expect(heap).must_be_instance_of MinHeap end @@ -52,30 +52,30 @@ end it "can remove nodes in the proper order" do - # Arrange - heap.add(3, "Pasta") - heap.add(6, "Soup") - heap.add(1, "Pizza") - heap.add(0, "Donuts") - heap.add(16, "Cookies") - heap.add(57, "Cake") + # Arrange + heap.add(3, "Pasta") + heap.add(6, "Soup") + heap.add(1, "Pizza") + heap.add(0, "Donuts") + heap.add(16, "Cookies") + heap.add(57, "Cake") - # Act - removed = heap.remove + # Act + removed = heap.remove - # Assert - expect(removed).must_equal "Donuts" + # Assert + expect(removed).must_equal "Donuts" - # Another Act - removed = heap.remove + # Another Act + removed = heap.remove - # Another assert - expect(removed).must_equal "Pizza" + # Another assert + expect(removed).must_equal "Pizza" - # Another Act - removed = heap.remove + # Another Act + removed = heap.remove - # Another assert - expect(removed).must_equal "Pasta" + # Another assert + expect(removed).must_equal "Pasta" end -end \ No newline at end of file +end diff --git a/test/test_helper.rb b/test/test_helper.rb index fddd934..2c4b52a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,9 +1,9 @@ -require 'minitest' -require 'minitest/autorun' -require 'minitest/reporters' +require "minitest" +require "minitest/autorun" +require "minitest/reporters" require "minitest/skip_dsl" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -require_relative '../lib/min_heap' -require_relative '../lib/heap_sort' +require_relative "../lib/min_heap" +require_relative "../lib/heap_sort" From 57044cae00013a6570e2bd1e04320eba4adaa8e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannah=20Teki=C3=A9?= Date: Mon, 4 Jan 2021 18:50:06 -0800 Subject: [PATCH 2/3] worked the 2 function with classmate --- lib/min_heap.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index e511bac..e20c1a1 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -87,13 +87,28 @@ def empty? # Time complexity: ? # Space complexity: ? def heap_up(index) + return nil if @store.empty? + predecessor_index = (index - 1)/2 + if @store[index].key >= @store[predecessor_index].key || index == 0 + return @store + else + swap(index, predecessor_index) + heap_up(predecessor_index) + 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) - + min = index + left, right = 2 * index + 1, 2 * index + 2 + left < @store.length && @store[left].key < @store[min].key ? min = left : nil + right < @store.length && @store[right].key < @store[min].key ? min = right : nil + if min != index + swap(index, min) + heap_down(min) + end end # If you want a swap method... you're welcome From 8d7238fcf19e18dd7e471187fbb4e043c8e3d739 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannah=20Teki=C3=A9?= Date: Tue, 5 Jan 2021 11:13:17 -0800 Subject: [PATCH 3/3] finished the heap_down --- lib/min_heap.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index e20c1a1..87210d1 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -88,12 +88,12 @@ def empty? # Space complexity: ? def heap_up(index) return nil if @store.empty? - predecessor_index = (index - 1)/2 - if @store[index].key >= @store[predecessor_index].key || index == 0 + parent_index = (index - 1)/2 + if @store[index].key >= @store[parent_index].key || index == 0 return @store else - swap(index, predecessor_index) - heap_up(predecessor_index) + swap(index, parent_index) + heap_up(parent_index) end end