-
Notifications
You must be signed in to change notification settings - Fork 5
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
Ringo - Earth #1
base: master
Are you sure you want to change the base?
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 Ringo, you hit all the learning goals here. Well done.
lib/heapsort.js
Outdated
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 But the space complexity of your solution is O(n) because you're building a heap here.
// 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) { |
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: 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() { |
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: O(1), just checks array length. | ||
// Space complexity: O(1), doesn't need any additional data structures. | ||
isEmpty() { |
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: 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) { |
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.
👍
if (this.store[parentIndex].key < this.store[index].key) return; | ||
|
||
this.swap(index, parentIndex); | ||
this.heapUp(parentIndex); | ||
} | ||
|
||
// This helper method takes an index and | ||
// moves it up the heap if it's smaller | ||
// than it's parent node. | ||
heapDown(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.
👍 , nicely compact!
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up
&heap_down
methods useful? Why?