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

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

require_relative 'min_heap'

# This method uses a heap to sort an array.
# Time Complexity: ?
# Space Complexity: ?
def heap_sort(list)
raise NotImplementedError, "Method not implemented yet..."
# Time Complexity: O(nlogn)
# Space Complexity: O(1)
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.

Outstanding work! You got the in-place O(1) space complexity Heapsort. Well done!

size = list.length()
size.times do |index|
heap_up(list, index)
end

size.times do |index|
swap(list, 0, size - 1 - index)
heap_down(list, size - 1 - index)
end
return list
end

def heap_up(list, index)
current = index
parent_index = (current % 2 == 0) ? current/2 - 1 : current/2
while current > 0 && list[current] > list[parent_index]
swap(list, current, parent_index)
current = parent_index
parent_index = (current % 2 == 0) ? current/2 - 1 : current/2
end
end

def heap_down(list, last)
current = 0
left = 1
while (current < last) && (left < last)
bigger = left
right = left + 1
bigger = right if right < last && list[right] > list[left]

break if list[current] > list[bigger]
swap(list, current, bigger)
current = bigger
left = 2*current + 1
end
end

def swap(list, index_1, index_2)
temp = list[index_1]
list[index_1] = list[index_2]
list[index_2] = temp
end
83 changes: 53 additions & 30 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
@@ -1,73 +1,96 @@
class HeapNode
attr_reader :key, :value

def initialize(key, value)
@key = key
@value = value
end
end

class MinHeap

def initialize
@store = []
end

# This method adds a HeapNode instance to the heap
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(logn)
# Space Complexity: O(1)
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.length() - 1)
end

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

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..."
swap(0, @store.length - 1)
element = @store.pop()
heap_down(0)
return element.value
end


# Used for Testing
def to_s
return "[]" if @store.empty?

output = "["
(@store.length - 1).times do |index|
output += @store[index].value + ", "
end

output += @store.last.value + "]"

return output
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 +53 to 55

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 @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(logn)
# Space complexity: O(1)
def heap_up(index)
Comment on lines +64 to 66

Choose a reason for hiding this comment

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

👍


parent = (index % 2 == 0) ? index/2 - 1 : index/2
while index > 0 && @store[index].key < @store[parent].key
swap(index, parent)
index = parent
parent = (index % 2 == 0) ? index/2 - 1 : index/2
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..."
end
# moves it down the heap if it's larger
# than it's child node.
def heap_down(current)

Choose a reason for hiding this comment

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

👍

left = 1
size = @store.length
while (current < size) && (left < size)
smaller = left
right = left + 1

smaller = right if right < size && @store[right].key < @store[left].key

break if @store[current].key < @store[smaller].key
swap(current, smaller)
current = smaller
left = 2*current + 1
end
end

# If you want a swap method... you're welcome
def swap(index_1, index_2)
temp = @store[index_1]
Expand Down
13 changes: 12 additions & 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 Expand Up @@ -33,4 +33,15 @@
# Assert
expect(result).must_equal [-50, 3, 5, 16, 27]
end

it "can sort a big array" do
# Arrange
list = [1, 12, 13, -50, 235, 2, 0, 9, -6, 7, 20, -12, 8, 31]

# Act
result = heapsort(list)

# Assert
expect(result).must_equal [-50, -12, -6, 0, 1, 2, 7, 8, 9, 12, 13, 20, 31, 235]
end
end