@@SzrIsTaken786 after that, everytime it would take o(1)[if elements did not change] or O( log n ) , while in ordinary array it would take o(n). For repeated action the efficiency is much better.
Time-wise it's the same, but plain merge sort requires O(n) space, and in-place merge sort becomes much more complicated than heap sort to implement and still requires either some extra space or a speed penalty
They're both n log n so technically they scale the same, but in heap sort we need to do an additional scan through the array which doesn't change much but is a bit of an extra step Maybe that's why most languages implement merge sort in their sort functions/methods in their language
You can just use the space the array is already using if you want O(1) space. You can treat a flat array as a heap with a root at 0 and child nodes at 2n+1 and 2n+2.
@@jewsefjones The O(1) solution is just the implementation of a heap on the existing array. To show that implementation would not really be possible in a short, but if you write your own version of heapify and heappop to happen in place, his solution becomes the O(1) one.
You're not using extra space it's the same array even if it looks like a tree you can actually make it an array where node k has 2k and 2k + 1 as its children and node j has j/2 as its parent
Master Data Structures & Algorithms For FREE at AlgoMap.io!
I got multiple offers after watching your videos. Now I’m just watching you 😅
"How come someone so smart is also so handsome" - all the chicks 😂
I would have needed that 3 Days ago for an exam, youtube algorithm you have failed me
"Handsome programmer like myself"🗿
O(n) is faster than O(n logn ) right...?
yup this video was a bad example for why heap sort is good
to find the max value of an array using max heap it's O(1) since it's the root element.
@@abcdE_OO7007 u missed the point. The heapify algorithm (how an array is turned into a heap) is O(n log n) so the big O will be O(n log n)
@@SzrIsTaken786 after that, everytime it would take o(1)[if elements did not change] or O( log n ) , while in ordinary array it would take o(n).
For repeated action the efficiency is much better.
@@abcdE_OO7007once the array is sorted it will only take O(1) to get that element??
Why not do a merge sort (O n log n) and take the first element? It's same as heap sort right?
Time-wise it's the same, but plain merge sort requires O(n) space, and in-place merge sort becomes much more complicated than heap sort to implement and still requires either some extra space or a speed penalty
Still has no time
How
I wonder if merge sort is better than this. Also good work appreciate your knowledge
they're the same time complexity so in that metric, they're equivalent
@@arenmee540 right 👍 thanks
They're both n log n so technically they scale the same, but in heap sort we need to do an additional scan through the array which doesn't change much but is a bit of an extra step
Maybe that's why most languages implement merge sort in their sort functions/methods in their language
Merge sort also have N*logN
Yes
Or I can use the max()
Question isn't your code o(n) space as you create a new list of size n?
ya, shouldn't space complexity be O(n) for result array?
It says right on the screen, space complexity of his implementation is O(n), but O(1) is _possible_ with a more complex implementation not shown here.
@@alxjones exactly why talk a o(1) solution then not give it to us :(
You can just use the space the array is already using if you want O(1) space. You can treat a flat array as a heap with a root at 0 and child nodes at 2n+1 and 2n+2.
@@jewsefjones The O(1) solution is just the implementation of a heap on the existing array. To show that implementation would not really be possible in a short, but if you write your own version of heapify and heappop to happen in place, his solution becomes the O(1) one.
How its O(1) ?
You're not using extra space it's the same array even if it looks like a tree you can actually make it an array where node k has 2k and 2k + 1 as its children and node j has j/2 as its parent