Skip to content
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
23 changes: 19 additions & 4 deletions lib/heap_sort.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@


# This method uses a heap to sort an array.
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(m+n)
# Space Complexity: O(n) - will have a call stack equal to number of nodes, though we are make a constant number of data structures... maybe O(1)
def heap_sort(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.

👍 The time complexity will be O(n log n) because you are adding n elements to the heap and each addition will take Log n time.

Then you remove n elements (each taking log n time).

raise NotImplementedError, "Method not implemented yet..."
end
# raise NotImplementedError, "Method not implemented yet..."
# left_child = i * 2 + 1
# right_child = i * 2 + 2
heap = MinHeap.new

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

sorted_list = []
until heap.empty?
sorted_list.push(heap.remove)
end

return sorted_list

end
62 changes: 50 additions & 12 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,26 @@ def initialize
end

# This method adds a HeapNode instance to the heap
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(m+1), where m is height - O(1) for adding, O(m) for heap up
# Space Complexity: O(1) - no new data structures made, but O(m) for heap up call stack (is it the number of calls in the calls stack or number of structures made?)
def add(key, value = key)
Comment on lines +17 to 19

Choose a reason for hiding this comment

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

👍 , However the time complexity is O(log n) because heap_up will go level-by-level in the tree. Since the tree is always balanced, m = log n.

raise NotImplementedError, "Method not implemented yet..."
@store << HeapNode.new(key, value)

if @store.length > 1
heap_up(@store.length - 1)
end

end

# This method removes and returns an element from the heap
# maintaining the heap structure
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n+1)
# Space Complexity: O(1)
def remove()
Comment on lines +30 to 32

Choose a reason for hiding this comment

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

👍 However the time complexity will be O(log n) time (because of the recursive swaps) and space complexity of O(log n) as well due to the call stack.

raise NotImplementedError, "Method not implemented yet..."
swap(0, -1)
popped = @store.pop
heap_down(0)
return popped.value
end


Expand All @@ -44,31 +52,61 @@ 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?
raise NotImplementedError, "Method not implemented yet..."

if @store.nil? || @store.length == 0
return true
else
return false
end

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(m) where m is height
# Space complexity: O(1), no persistent data structure is made beyond 1 temporary reassigned variable
def heap_up(index)
Comment on lines +72 to 74

Choose a reason for hiding this comment

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

👍 , but I would give O(log n) for the time complexity because the tree is always balanced and the height is always log n. The space complexity would be O(log n) due to the call stack (recursion).

return if index == 0

if @store[index].key < @store[((index - (index % 3)) / 2)].key

Choose a reason for hiding this comment

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

It might be easier just to do

parent_index = (index - 1) / 2

And use that variable

temp = @store[index]
@store[index] = @store[((index - (index % 3)) / 2)]
@store[((index - (index % 3)) / 2)] = temp
end

heap_up(((index - (index % 3)) / 2))

end

# This helper method takes an index and
# moves it up the heap if it's smaller
# than it's parent node.
# left_child = i * 2 + 1
# right_child = i * 2 + 2
def heap_down(index)
raise NotImplementedError, "Method not implemented yet..."
return if @store[index].nil?

if !@store[index * 2 + 1].nil? && @store[index].key > @store[index * 2 + 1].key
swap(index, (index * 2 + 1))
end

if !@store[index * 2 + 2].nil? && @store[index].key > @store[index * 2 + 2].key
swap(index, (index * 2 + 2))
end

heap_down(index * 2 + 1)
heap_down(index * 2 + 2)
Comment on lines +95 to +104

Choose a reason for hiding this comment

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

You're doing a bunch of extra swaps here. Instead find the smallest child and then if it's smaller than the parent, swap those and continue. You should never need to swap both the right and left children, as well as heap down both children.


end

# If you want a swap method... you're welcome
#THANK YOUUUUUU

Choose a reason for hiding this comment

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

😻

def swap(index_1, index_2)
temp = @store[index_1]
@store[index_1] = @store[index_2]
Expand Down
8 changes: 4 additions & 4 deletions test/heapsort_test.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require_relative "test_helper"

xdescribe "heapsort" do
describe "heapsort" do
it "sorts an empty array" do
# Arrange
list = []

# Act
result = heapsort(list)
result = heap_sort(list)

# Assert
expect(result).must_equal []
Expand All @@ -17,7 +17,7 @@
list = [5]

# Act
result = heapsort(list)
result = heap_sort(list)

# Assert
expect(result).must_equal [5]
Expand All @@ -28,7 +28,7 @@
list = [5, 27, 3, 16, -50]

# Act
result = heapsort(list)
result = heap_sort(list)

# Assert
expect(result).must_equal [-50, 3, 5, 16, 27]
Expand Down
40 changes: 20 additions & 20 deletions test/min_heap_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
end
# Another assert
expect(removed).must_equal "Pasta"
end
end