Open
Conversation
CheezItMan
reviewed
May 1, 2021
CheezItMan
left a comment
There was a problem hiding this comment.
Nice work Ringo, you hit all the learning goals here. Well done.
Comment on lines
+4
to
15
| // Time Complexity: O(n log n). The while loops both run for | ||
| // every element in list, so on their own have O(n). Within them | ||
| // they call either heap.add or heap.remove, which has O(log n) | ||
| // time complexity. | ||
| // Space Complexity: O(1). The function does make a new data structure -- | ||
| // a heap -- but it only adds to the heap one element for every element it | ||
| // pops off of the list, so the list is taking less space as the heap takes | ||
| // more. Then, the process is reversed with the heap taking less space | ||
| // so the list can be rebuilt and take more space. Increasing the size of | ||
| // the list is not going to change the additional amount of space the function uses. | ||
|
|
||
| function heapsort(list) { |
There was a problem hiding this comment.
👍 But the space complexity of your solution is O(n) because you're building a heap here.
Comment on lines
+14
to
18
| // Time Complexity: O(log n). Pushing the new value to array is O(1), | ||
| // then it calls heapUp which is O(log n). | ||
| // Space Complexity: O(log n). The space for actually adding is O(1), | ||
| // but it calls heapUp which is O(log n). | ||
| add(key, value = key) { |
Comment on lines
+25
to
29
| // Time Complexity: O(log n). The swap and pop are both O(1), | ||
| // then it calls heapDown which is O(log n). | ||
| // Space Complexity: O(log n). The space for removing is O(1), | ||
| // but it calls heapDown which is O(log n). | ||
| remove() { |
Comment on lines
+52
to
54
| // Time complexity: O(1), just checks array length. | ||
| // Space complexity: O(1), doesn't need any additional data structures. | ||
| isEmpty() { |
Comment on lines
+61
to
69
| // Time complexity: O(log n). The calculation, swap, and comparison | ||
| // are all constant, but the function will need to be called as | ||
| // many times as there are levels in the heap, which scales | ||
| // logarithmically with the heap's size. | ||
| // Space complexity: O(log n). Since it is a recursive function | ||
| // it will have at worst as many function calls on the stack | ||
| // as times it will be called, which is controlled by number of levels | ||
| // in the heap and scales logarithmically with size. | ||
| heapUp(index) { |
| // This helper method takes an index and | ||
| // moves it up the heap if it's smaller | ||
| // than it's parent node. | ||
| heapDown(index) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up&heap_downmethods useful? Why?