-
Notifications
You must be signed in to change notification settings - Fork 40
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
base: master
Are you sure you want to change the base?
Branches - Eve #16
Changes from all commits
b515db2
91e1aa8
54f7ca2
a9c7b5f
3308ccf
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,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) | ||
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 |
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
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..." | ||
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
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..." | ||
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
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.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
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. 👍 |
||
|
||
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) | ||
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. 👍 |
||
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] | ||
|
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.
Outstanding work! You got the in-place O(1) space complexity Heapsort. Well done!