Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Space - Jessica #16

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions lib/heap_sort.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@

require_relative "min_heap"

# This method uses a heap to sort an array.
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(nlogn) - loops n times based on length of list, and then logn adding or removing
# Space Complexity: O(nlogn)
def heapsort(list)
Comment on lines +4 to 6

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 However your space complexity is O(n) because you add n elements into the heap.

raise NotImplementedError, "Method not implemented yet..."
return list if list.length <= 1

heap = MinHeap.new

list.each do |i|
heap.add(i)
end

ordered_list = []
list.length.times do |i|
ordered_list << heap.remove.value
end

return ordered_list
end
70 changes: 59 additions & 11 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,45 @@ def initialize
@store = []
end

# helper method to find parent
def find_parent(index)
return (index - 1) / 2
end

# helper method to find left child
def left_child(index)
return 2 * index + 1
end

# helper method to find right child
def right_child(index)
return 2 * index + 2
end
Comment on lines +17 to +29

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good set of helper methods.


# This method adds a HeapNode instance to the heap
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(logn) depending on the height of the heap
# Space Complexity: O(logn) to hold the recursive stack
def add(key, value = key)
Comment on lines +32 to 34

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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
# maintaining the heap structure
# Time Complexity: ?
# Space Complexity: ?
def remove()
raise NotImplementedError, "Method not implemented yet..."
# Time Complexity: O(logn) depending on the height of the heap
# Space Complexity: O(logn) to hold the recursive stack
def remove
Comment on lines +41 to +43

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if @store.empty?
return nil
end

swap(0, @store.length - 1)
result = @store.pop

heap_down(0) unless @store.empty?
return result
end


# Used for Testing
def to_s
return "[]" if @store.empty?
Expand All @@ -55,17 +78,42 @@ 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(logn)
def heap_up(index)
Comment on lines +81 to 83

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return if index == 0

parent = find_parent(index)
if @store[parent].key > @store[index].key
swap(parent, index)
heap_up(parent)
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)
Comment on lines 93 to 96

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError, "Method not implemented yet..."
return if index >= @store.length-1

left = left_child(index)
right = right_child(index)

return if !@store[left] && !@store[right]

smallest = nil
if !@store[left]
smallest = right
Comment on lines +105 to +106

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note, that if left is not in the array, then right is not either.

elsif !@store[right]
smallest = left
else
smallest = (@store[left].key < @store[right].key) ? left : right
end

if @store[index].key > @store[smallest].key
swap(index, smallest)
heap_down(smallest)
end
end

# If you want a swap method... you're welcome
Expand Down
2 changes: 1 addition & 1 deletion test/heapsort_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative "test_helper"

xdescribe "heapsort" do
describe "heapsort" do
it "sorts an empty array" do
# Arrange
list = []
Expand Down
6 changes: 3 additions & 3 deletions test/min_heap_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@
heap.add(57, "Cake")

# Act
removed = heap.remove
removed = heap.remove.value

# Assert
expect(removed).must_equal "Donuts"

# Another Act
removed = heap.remove
removed = heap.remove.value

# Another assert
expect(removed).must_equal "Pizza"

# Another Act
removed = heap.remove
removed = heap.remove.value

# Another assert
expect(removed).must_equal "Pasta"
Expand Down