4 ms·
I'm confused: Does top.execute() modify currentQueue in some way, like pushing new elements to it? If it doesn't, then why not simply move the sort out of the l
by adwn 1y ago
I'm confused: Does top.execute() modify currentQueue in some way, like pushing new elements to it?
If it doesn't, then why not simply move the sort out of the loop? This is simpler and faster than maintaining a binary heap.
- adwn 1y agoOne more thing: Nowadays sort() functions ary usually heavily optimized and recognize already sorted subsequences. If currentQueue isn't modified during the loop, then the sort() call should run in O(n) after the first iteration, instead of O(n * log n). Still worse than not having it inside the loop at all, of course.
- nateb2022 1y ago> If it doesn't, then why not simply move the sort out of the loop? Yup, they should definitely move the sort outside of the loop. Shifting is O(N) so overall complexity would be O(N^2) but they could avoid shifting by reverse-sorting outside the loop and then iterating backwards using pop()