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
48 changes: 45 additions & 3 deletions lib/heap_sort.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,50 @@


# This method uses a heap to sort an array.
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n log n)
# Space Complexity: 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.

👍 , Nice work doing this in-place with heapsort. Using the given array as the heap. Well done.

raise NotImplementedError, "Method not implemented yet..."
n = list.size
return list if n < 2

# heap construction - reorganize original array into a maximum heap
# proceed from right to left, starting halfway back through the array
# O(n)
i = (n - 1) / 2
while i >= 0
heap_down(list, i, n)
i -= 1
end

# sort
# remove the largest remaining item from the heap and
# put it into the array position vacated as the heap shrinks
# O(n log n)
while n > 0
list[0], list[n - 1] = list[n - 1], list[0]

n -= 1
heap_down(list, 0, n)

end
list
end


# max heap heap_down method
def heap_down(list, index, n)
while index * 2 + 1 < n
j = index * 2 + 1 # first child

if j < n - 1 && list[j] < list[j + 1] # greater of two children
j += 1
end

if list[index] > list[j] # stop if move will violate heap order
break
end

list[index], list[j] = list[j], list[index]
index = j
end
end
53 changes: 40 additions & 13 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,28 @@ def initialize
end

# This method adds a HeapNode instance to the heap
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(log n) - worst case, when need heap-up, O(1) - best case
# Space Complexity: O(log n) - worst case, when need heap-up, O(1) - best case
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.

👍

raise NotImplementedError, "Method not implemented yet..."
new_node = HeapNode.new(key, value)
@store << new_node
heap_up(@store.size-1)
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(log n)
def remove()
Comment on lines +27 to 29

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 @store.empty?
if @store.size == 1
return @store.pop
end
# swap the first and the last element in the underlying array
@store[-1], @store[0] = @store[0], @store[-1]
removed = @store.pop
heap_down(0)
return removed.value
end


Expand All @@ -44,28 +54,45 @@ 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?
Comment on lines +57 to 59

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.empty?
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(log n)
def heap_up(index)
Comment on lines +68 to 70

Choose a reason for hiding this comment

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

👍


# base case
parent_index = (index - 1) / 2
return if parent_index < 0 || @store[index].key >= @store[parent_index].key

# swap
@store[index], @store[parent_index] = @store[parent_index], @store[index]
# recursive case
heap_up(parent_index)
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 81 to 84

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..."
n = @store.size
while index * 2 + 1 < n
j = index * 2 + 1

j += 1 if j < n - 1 && @store[j + 1].key < @store[j].key

Choose a reason for hiding this comment

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

Nice work handling the left and right children here.


break if @store[index].key <= @store[j].key

@store[index], @store[j] = @store[j], @store[index]
index = j
end
end

# If you want a swap method... you're welcome
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