-
Notifications
You must be signed in to change notification settings - Fork 37
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
Water - Kareha #17
base: master
Are you sure you want to change the base?
Water - Kareha #17
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,67 @@ | ||
require_relative 'min_heap' | ||
|
||
def swap(index1, index2, list) | ||
list[index1], list[index2] = list[index2], list[index1] | ||
end | ||
|
||
# for max heap | ||
# this is helpful https://www.youtube.com/watch?v=2DmK_H7IdTo | ||
# this is also helpful: https://www.programiz.com/dsa/heap-sort | ||
# time: o(logn) | ||
# space: o(logn) - because of call stack? | ||
def heap_down(list, i, length) | ||
left_child = (i * 2) + 1 | ||
right_child = (i * 2) + 2 | ||
max = i | ||
|
||
if left_child < length && list[left_child] > list[max] | ||
max = left_child | ||
end | ||
|
||
if right_child < length && list[right_child] > list[max] | ||
max = right_child | ||
end | ||
|
||
if i != max | ||
swap(i, max, list) | ||
heap_down(list, max, length) | ||
end | ||
|
||
end | ||
|
||
|
||
# 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(logn) - bc of call stack? | ||
def heapsort(list) | ||
# heap = MinHeap.new() | ||
|
||
# until list.empty? # time: o(n) | ||
# heap.add(list.pop) # time: o(logn) heap add, o(1) list.pop, space: o(n) | ||
# end | ||
|
||
# until heap.empty? # time: ? | ||
# list.push(heap.remove) # time: o(logn) heap remove, o(1) list.push, space: o(n) | ||
# end | ||
|
||
# return list | ||
|
||
end_index = list.length - 1 | ||
while end_index >= 0 # time: o(n) | ||
# heapify - build a max heap | ||
# parent nodes start at length / 2 | ||
parent_index = end_index/2 | ||
while parent_index >= 0 | ||
heap_down(list, parent_index, end_index) # time: o(logn) | ||
parent_index -= 1 | ||
end | ||
|
||
# move largest element to the end | ||
swap(0, end_index, list) | ||
|
||
# don't include sorted portion in further heap down | ||
end_index -= 1 | ||
end | ||
|
||
return list | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,18 +14,24 @@ def initialize | |
end | ||
|
||
# This method adds a HeapNode instance to the heap | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(logn) | ||
# Space Complexity: O(logn) | ||
def add(key, value = key) | ||
Comment on lines
+17
to
19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
raise NotImplementedError, "Method not implemented yet..." | ||
node = HeapNode.new(key, value) | ||
@store.push(node) | ||
return 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(logn) | ||
def remove() | ||
Comment on lines
+27
to
29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
raise NotImplementedError, "Method not implemented yet..." | ||
return if @store.empty? | ||
swap(0, -1) | ||
removed = @store.pop | ||
heap_down(0) unless @store.empty? | ||
return removed.value | ||
end | ||
|
||
|
||
|
@@ -44,28 +50,52 @@ 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
+53
to
55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
raise NotImplementedError, "Method not implemented yet..." | ||
return @store[0].nil? | ||
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(logn) | ||
def heap_up(index) | ||
Comment on lines
+64
to
66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 nice! |
||
|
||
return if index == 0 | ||
|
||
parent = (index - 1)/2 | ||
if @store[parent].key > @store[index].key | ||
swap(parent, index) | ||
else | ||
return | ||
end | ||
|
||
return heap_up(parent) | ||
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
79
to
82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 , Nicely done! |
||
raise NotImplementedError, "Method not implemented yet..." | ||
left_child = (index * 2) + 1 | ||
right_child = (index * 2) + 2 | ||
|
||
# get child index | ||
if @store[left_child] && @store[right_child] | ||
child_index = @store[left_child].key < @store[right_child].key ? left_child : right_child | ||
elsif @store[left_child] # no case where just right and no left | ||
child_index = left | ||
else # both are null so return | ||
return | ||
end | ||
|
||
if @store[index].key > @store[child_index].key | ||
swap(index, child_index) | ||
return heap_down(child_index) | ||
end | ||
end | ||
|
||
# If you want a swap method... you're welcome | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Nice work for in-place heapsort!