-
Notifications
You must be signed in to change notification settings - Fork 51
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
Mariah: Pine #19
base: master
Are you sure you want to change the base?
Mariah: Pine #19
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.
💫 Nice work Mariah! I just had a couple comments below. Let me know what questions you have.
🟢
|
||
|
||
def heap_sort(list): | ||
""" This method uses a heap to sort an array. | ||
Time Complexity: ? | ||
Space Complexity: ? | ||
Time Complexity: O(log n) |
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.
✨ Would love to see how you could write heap_sort
with the MinHeap
class you implemented below as well. Additionally, time complexity is O(n logn) because you are doing heappush
/heappop
which is a log(n) operation n times.
while parent >= 0 and self.store[parent].key > self.store[temp].key: | ||
self.swap(temp, parent) | ||
temp = parent | ||
parent = (temp - 1) // 2 | ||
|
||
def heap_down(self, index): | ||
""" This helper method takes an index and | ||
moves the corresponding element down the heap if it's | ||
larger than either of its children and continues until | ||
the heap property is reestablished. | ||
""" |
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.
✨
Time complexity: ? | ||
Space complexity: ? | ||
Time complexity: O(log n) | ||
Space complexity: O(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.
✨ Nice iterative solution! The only thing I would say is style wise, I might stick to recursion or iteration across heap_up
and heap_down
for consistency.
Time complexity: ? | ||
Space complexity: ? | ||
Time complexity: O(1) | ||
Space complexity: O(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.
✨
Time Complexity: ? | ||
Space Complexity: ? | ||
Time Complexity: O(log n) | ||
Space Complexity: O(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.
✨ Space complexity is O(log n) here because of the recursive call stack of heap_down
Time Complexity: ? | ||
Space Complexity: ? | ||
Time Complexity: O(log n) | ||
Space Complexity: O(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.
✨
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up
&heap_down
methods useful? Why?