Conversation
CheezItMan
left a comment
There was a problem hiding this comment.
Not bad, take a look at my comments and let me know what questions you have. In particular when you heap up and down you need to compare the values involved to make sure if you need to swap or not.
| @@ -1,8 +1,24 @@ | |||
|
|
|||
| require 'pry' | |||
There was a problem hiding this comment.
You can take out pry when submitting things.
| # This method uses a heap to sort an array. | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) followed by O(log n) beacause going through entire array for first operation |
There was a problem hiding this comment.
It's actually O(n log n) which is because you're adding n elements to the heap which each take log n time. Then you remove n elements which takes log n time each.
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) followed by O(log n) beacause going through entire array for first operation | ||
| # Space Complexity: I am attempting to do it in O(1) |
There was a problem hiding this comment.
Since you're creating a heap and building it up. This is O(n) space complexity.
| elsif @store[child_index_right].nil? | ||
| swap(index, child_index_left) |
There was a problem hiding this comment.
You should always compare the element at index with child_index_left. This applies every place in the heap.
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up&heap_downmethods useful? Why?