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

require 'pry'

Choose a reason for hiding this comment

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

You can take out pry when submitting things.

require_relative 'min_heap'

# This method uses a heap to sort an array.
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) followed by O(log n) beacause going through entire array for first operation

Choose a reason for hiding this comment

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

It's actually O(n log n) which is because you're adding n elements to the heap which each take log n time. Then you remove n elements which takes log n time each.

# Space Complexity: I am attempting to do it in O(1)

Choose a reason for hiding this comment

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

Since you're creating a heap and building it up. This is O(n) space complexity.

def heap_sort(list)
raise NotImplementedError, "Method not implemented yet..."
if list.nil? || list.empty? || list.length == 1
return list
end

min_heap = MinHeap.new

until list.empty?
min_heap.add(list.pop)
end

until min_heap.empty?
retrieved = min_heap.remove
list.push(retrieved)
end

return list
end
65 changes: 49 additions & 16 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'pry'
class HeapNode
attr_reader :key, :value

Expand All @@ -14,18 +15,26 @@ def initialize
end

# This method adds a HeapNode instance to the heap
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(log n) bc it's a tree
# Space Complexity: O(1)
def add(key, value = key)
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(log n) bc it's a tree
# Space Complexity: O(n log n) bc of recursive calls
def remove
index = 0
return nil if @store.empty?

swap(0, -1)
return_value = @store.pop
heap_down(index)

return return_value.value
end


Expand All @@ -44,28 +53,52 @@ def to_s
end

# This method returns true if the heap is empty
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(1) - Uncertain if Ruby under the hood implements an array in the strictest sense, then by inference it allocates a certain number of spaces to an array and retrieving the length would be constant. Otherwise it is O(n) due to check each element until it becomes nil.
# Space complexity: O(1)
def empty?
raise NotImplementedError, "Method not implemented yet..."
return @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: ?
# 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: O(logn)
# Space complexity: Constant
def heap_up(index)

while index > 0
parent_index = ((index - 1) / 2)
if @store[parent_index].key > @store[index].key
swap(parent_index, index)
index = parent_index
else
return
end
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)
raise NotImplementedError, "Method not implemented yet..."
child_index_left = ((index * 2) + 1)
child_index_right = ((index * 2) + 2)

#if there are no children, return
if @store[child_index_left].nil?
return
elsif @store[child_index_right].nil?
swap(index, child_index_left)
Comment on lines +91 to +92

Choose a reason for hiding this comment

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

You should always compare the element at index with child_index_left. This applies every place in the heap.

else
if @store[child_index_left].key < @store[child_index_right].key
swap(index, child_index_left)
heap_down(child_index_left)
else
swap(index, child_index_right)
heap_down(child_index_right)
end
end
end

# If you want a swap method... you're welcome
Expand Down
30 changes: 30 additions & 0 deletions test/min_heap_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,34 @@
# Another assert
expect(removed).must_equal "Pasta"
end
end

describe "heap sort" do
it "can handle a null list" do
result = heap_sort(nil)
assert_nil(result)
end

it "can handle an empty list" do
list = []
result = heap_sort(list)

assert_empty(result)
end

it "can handle a list with one element" do
list = [10]
result = heap_sort(list)

expect(result).must_equal list
end

it "can sort a list in ascending order using heap sort" do
list = [5, 3, 1, 7]
min_result = [1, 3, 5, 7]

sorted_result = heap_sort(list)

expect(sorted_result).must_equal(min_result)
end
end