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

Savannah #28

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


# This method uses a heap to sort an array.
# Time Complexity: ?
# Space Complexity: ?
def heap_sort(list)
raise NotImplementedError, "Method not implemented yet..."
end
# Time Complexity: O(nlogn)
# Space Complexity: O(n)

Choose a reason for hiding this comment

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

Correct by how you're using recursion to transform list into a heap.

def heapsort(list)
heapify(list, parent(list.length - 1)) if list.length > 1
sort_heaped_list(list)
return list
end

private
# Time Complexity: O(nlog(n))
# Space Complexity: O(1)
def sort_heaped_list(list)
last_pos = list.length - 1
while last_pos > 0
remove_max(list, last_pos)
last_pos -= 1
end
end

# Time Complexity: O(log(n))
# Space Complexity: O(1)
def remove_max(list, last_pos)
removed = list[0]
list[0] = list[last_pos]
list[last_pos] = removed
heap_down(0, last_pos - 1, list)
end

# Time Complexity: O(log(n))
# Space Complexity: O(1)
def heap_down(sub_root, last_pos, list)
max_child = max_child(right_child(sub_root, last_pos), left_child(sub_root, last_pos), list)
while max_child && list[max_child] > list[sub_root]
swap(sub_root, max_child, list)
sub_root = max_child
max_child = max_child(right_child(sub_root, last_pos), left_child(sub_root, last_pos), list)
end
end

# Time Complexity: O(n) => this is based of the math proof on geeks for geeks, not sure if I 100 % understand ... yet
# Space Complexity: O(n) <= can optimize by using iterative approach over recursion

Choose a reason for hiding this comment

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

Correct, you can optimize it a bit further using iteration.

def heapify(list, sub_root, counter = 0)
max_child = max_child(right_child(sub_root, list.length), left_child(sub_root, list.length), list)
if max_child && list[max_child] > list[sub_root]
swap(max_child, sub_root, list)
heapify(list, max_child, counter + 1)
elsif max_child
heapify(list, sub_root - 1, counter + 1)
end
return
end


# Time Complexity: O(1)
# Space Complexity: O(1)
def swap(index1, index2, list)
temp = list[index1]
list[index1] = list[index2]
list[index2] = temp
end

# Time Complexity: O(1)
# Space Complexity: O(1)
def max_child(left, right, list)
if !right || !left || list[left] > list[right]
return left
end
return right
end

# Time Complexity: O(1)
# Space Complexity: O(1)
def parent(child)
return (child - 1) / 2
end

# Time Complexity: O(1)
# Space Complexity: O(1)
def right_child(root, length)
child = root * 2 + 2
if child < length
return child
end
return
end

# Time Complexity: O(1)
# Space Complexity: O(1)
def left_child(root, length)
child = root * 2 + 1
if child < length
return child
end
return
end
75 changes: 62 additions & 13 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@ def initialize
end

# This method adds a HeapNode instance to the heap
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(log(n))
# Space Complexity: 0(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: ?
# Time complexity: O(log(n))
# Space complexity: O(1)
def remove()

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..."
removed = @store[0]
@store[0] = @store.pop
heap_down(0)
return removed.value
end


Expand All @@ -44,28 +48,73 @@ def to_s
end

# This method returns true if the heap is empty
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(1) assuming ruby implements array with memotized length ie attribute of array class rather than method
# 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: ?
# Time complexity: O(log(n))
# Space complexity: O(1)
def heap_up(index)

Choose a reason for hiding this comment

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

👍


parent_i = parent_index(index)
until index == 0 || @store[parent_i].key < @store[index].key
swap(parent_i, index)
index = parent_i
parent_i = parent_index(index)
end
end

def parent_index(index)
return (index - 1) / 2
end

# This helper method takes an index and
# moves it up the heap if it's smaller
# than it's parent node.
# Time complexity: O(log(n))
# Space complexity: O(1)
def heap_down(index)
raise NotImplementedError, "Method not implemented yet..."
right_child_i = right_child_index(index)
left_child_i = left_child_index(index)
until heaped_down_valid(left_child_i, right_child_i, index)
pos_to_swap = index_of_smallest_child_key(left_child_i, right_child_i)
swap(pos_to_swap, index)
index = pos_to_swap
right_child_i = right_child_index(index)
left_child_i = left_child_index(index)
end
end

def heaped_down_valid(left_child_i, right_child_i, index)
return !left_child_i || @store[left_child_i].key > @store[index].key && (right_child_i && @store[right_child_i].key > @store[index].key)
end

def index_of_smallest_child_key(index1, index2)
if !index2 || @store[index2].key > @store[index1].key
return index1
else
return index2
end
end

def right_child_index(index)
if index * 2 + 2 <= @store.length - 1
return index * 2 + 2
end
return
end

def left_child_index(index)
if index * 2 + 1 <= @store.length - 1
return index * 2 + 1
end
return
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