-
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
Janice H. #20
base: master
Are you sure you want to change the base?
Janice H. #20
Conversation
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.
Some serious issues here. You tried to do the in-place Heapsort and it's not working. See my inline notes there. You also have some errors in time/space complexity and your heap_down method is not working in a heap-like manner. See my notes.
# Time Complexity: O(2nlogn)? | ||
# Space Complexity: O(1) | ||
# GAH I cannot get those last two numbers | ||
# sorted properly for some reason and it's | ||
# time to turn this in. I know they are in | ||
# the correct order at the end but then flip | ||
def heapsort(list) |
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.
It's neat that you were trying to do the in-place heapsort. I'd suggest doing it the simple way first, using the minheap you created in this assignment and adding all the elements of the list to the heap and then extracting them one by one and shoveling them into the resulting list. It's O(n) space complexity but it works. Then try for this one if/when you have time.
return list | ||
end | ||
|
||
def heap_up(list, index) |
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.
To do this in-place, I would suggest making a Max_heap instead of a min-heap and then as you remove a node adding it to the rear of the array working your way from the last node until index 0. That's a lot easier than using a min-heap like you are doing.
# Time Complexity: O(nlogn) | ||
# Space Complexity: O(n) | ||
def add(key, value = key) |
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.
This works, but why would adding one value to the heap be O(n log n) time complexity and why would it take O(n) space complexity?
I suggest it's O(log n) for space (the recursive stack) and O(log n) for time.
# Time Complexity: O(nlogn) | ||
# Space Complexity: O(1) | ||
def remove() |
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.
See my notes above about space/time complexity.
def heap_up(index) | ||
|
||
# for right children | ||
if index % 2 == 0 |
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.
These if statements could be condensed a bit.
def heap_down(index) | ||
raise NotImplementedError, "Method not implemented yet..." | ||
if !@store[index+1].nil? && @store[index].key > @store[index+1].key | ||
swap(index, index+1) | ||
heap_down(index+1) | ||
elsif !@store[index+2].nil? && @store[index].key > @store[index+2].key | ||
swap(index, index+2) | ||
heap_down(index+2) | ||
end | ||
end |
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.
This method is NOT doing head_down. Instead it's comparing a node to the nodes next to it. Remember the formula to find a node's left child is:
left_child_index = index * 2 + 1
and the right child
right_child_index = index * 2 + 2
Because you're not doing the correct heap_down method this method is just using a sorted array which is O(n)
# Time complexity: O(nlogn) | ||
# Space complexity: O(1) | ||
def heap_up(index) |
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.
How is the time complexity to heap up 1 element O(n log n), you don't, or shouldn't look at every node in the list for this method.
Co-Authored-By: Chris M <[email protected]>
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up
&heap_down
methods useful? Why?