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

KS #9

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

KS #9

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.vscode/settings.json
18 changes: 13 additions & 5 deletions lib/heap_sort.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# Time Complexity: O(nlog(n))
# Space Complexity: Constant.
def heapsort(array)
heap = MinHeap.new

array.each do |item|
heap.add(item, item)
end

# This method uses a heap to sort an array.
# Time Complexity: ?
# Space Complexity: ?
def heap_sort(list)
raise NotImplementedError, "Method not implemented yet..."
result = []
until heap.empty?
result << item.remove
end

return result
end
74 changes: 46 additions & 28 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@ def initialize
@store = []
end

# This method adds a HeapNode instance to the heap
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: ? O(log n)
# Space Complexity: ? O(log n))
def add(key, value = key)
raise NotImplementedError, "Method not implemented yet..."
node = HeapNode.new(key, value)
@store << 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(log n)
# Space Complexity: O(log n)
def remove()
raise NotImplementedError, "Method not implemented yet..."
return nil if !@store

swap(0, @store.length - 1)
nope = @store.pop

heap_down(0)
return nope.value
end


Expand All @@ -39,39 +44,52 @@ def to_s
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?
raise NotImplementedError, "Method not implemented yet..."
!@store
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: ?
def heap_up(index)

# Time complexity: O(log n)
# Space complexity: Log n
def heap_up(i)
return if i == 0

pnode = (i-1) / 2
if @store[i].key < @store[pnode].key
swap(i, pnode)
heap_up(pnode)
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..."
def heap_down(i)
li = i*2+1
ri = i*2+2

if ri < @store.length

min = @store[li].key < @store[ri].key ? li : ri

if @store[i].key > @store[min].key
swap(i, min)
heap_down(min)
end
elsif li < @store.length
if @store[i].key > @store[li].key
swap(i, li)
end
end
end

# If you want a swap method... you're welcome
def swap(index_1, index_2)
temp = @store[index_1]
@store[index_1] = @store[index_2]
@store[index_2] = temp
end
end
end