-
Notifications
You must be signed in to change notification settings - Fork 41
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
Space - Jessica #16
base: master
Are you sure you want to change the base?
Space - Jessica #16
Changes from all commits
9b843cc
a02d29d
b46b13e
c93c297
d0700a4
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,21 @@ | ||
|
||
require_relative "min_heap" | ||
|
||
# This method uses a heap to sort an array. | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(nlogn) - loops n times based on length of list, and then logn adding or removing | ||
# Space Complexity: O(nlogn) | ||
def heapsort(list) | ||
raise NotImplementedError, "Method not implemented yet..." | ||
return list if list.length <= 1 | ||
|
||
heap = MinHeap.new | ||
|
||
list.each do |i| | ||
heap.add(i) | ||
end | ||
|
||
ordered_list = [] | ||
list.length.times do |i| | ||
ordered_list << heap.remove.value | ||
end | ||
|
||
return ordered_list | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,22 +13,45 @@ def initialize | |
@store = [] | ||
end | ||
|
||
# helper method to find parent | ||
def find_parent(index) | ||
return (index - 1) / 2 | ||
end | ||
|
||
# helper method to find left child | ||
def left_child(index) | ||
return 2 * index + 1 | ||
end | ||
|
||
# helper method to find right child | ||
def right_child(index) | ||
return 2 * index + 2 | ||
end | ||
Comment on lines
+17
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. Good set of helper methods. |
||
|
||
# This method adds a HeapNode instance to the heap | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(logn) depending on the height of the heap | ||
# Space Complexity: O(logn) to hold the recursive stack | ||
def add(key, value = key) | ||
Comment on lines
+32
to
34
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..." | ||
@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: ? | ||
def remove() | ||
raise NotImplementedError, "Method not implemented yet..." | ||
# Time Complexity: O(logn) depending on the height of the heap | ||
# Space Complexity: O(logn) to hold the recursive stack | ||
def remove | ||
Comment on lines
+41
to
+43
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. 👍 |
||
if @store.empty? | ||
return nil | ||
end | ||
|
||
swap(0, @store.length - 1) | ||
result = @store.pop | ||
|
||
heap_down(0) unless @store.empty? | ||
return result | ||
end | ||
|
||
|
||
# Used for Testing | ||
def to_s | ||
return "[]" if @store.empty? | ||
|
@@ -55,17 +78,42 @@ def empty? | |
# 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
+81
to
83
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. 👍 |
||
return if index == 0 | ||
|
||
parent = find_parent(index) | ||
if @store[parent].key > @store[index].key | ||
swap(parent, index) | ||
heap_up(parent) | ||
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) | ||
Comment on lines
93
to
96
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 index >= @store.length-1 | ||
|
||
left = left_child(index) | ||
right = right_child(index) | ||
|
||
return if !@store[left] && !@store[right] | ||
|
||
smallest = nil | ||
if !@store[left] | ||
smallest = right | ||
Comment on lines
+105
to
+106
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. Just a note, that if left is not in the array, then right is not either. |
||
elsif !@store[right] | ||
smallest = left | ||
else | ||
smallest = (@store[left].key < @store[right].key) ? left : right | ||
end | ||
|
||
if @store[index].key > @store[smallest].key | ||
swap(index, smallest) | ||
heap_down(smallest) | ||
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.
👍 However your space complexity is O(n) because you add n elements into the heap.