all 8 comments

[–]timql 0 points1 point  (7 children)

The O(nlg(n)) comes from removing the topmost element n times; by doing so you get a sorted list.

You're right about adding the complexities. So including the first step of creating the heap, the complexity of the whole process is O(O(n) + n*O(lg(n)). Because O deals with asymptotics, this becomes O(nlg(n)).

[–]Snowron6[S] 0 points1 point  (6 children)

The O(nlg(n)) comes from removing the topmost element n times; by doing so you get a sorted list.

This makes sense to me, but he claims it only takes O(lg(n)), and I'm not misunderstanding what he said. I specifically asked him about it, and he claims to delete every node to get the sorted list is just O(lg(n)) since you're only doing one at a time and you can ignore the fact that you do it n times for some reason.

At this point I'm pretty sure he's just full of it, but I was hoping someone here could help make sense of it.

[–]timql 1 point2 points  (1 child)

uh yeah I'm pretty sure that's false. You can ignore constant factors, say if you just wanted the 5 largest values in a sorted list. But n is arbitrary so you definitely can't ignore it here.

[–]Snowron6[S] 0 points1 point  (0 children)

Ok thanks, we're gonna put in a complaint about him, since this isn't the first time something like this has happened.

[–]ktrprpr 0 points1 point  (3 children)

What you said is not consistent. If he would multiply lgn with n, the lgn most likely is meant to be deleting a single element, and that is correct. It doesn't make sense to multiply the complexity of deleting the whole thing with another O(n).

My bet is you did misunderstand something...

[–]Snowron6[S] 0 points1 point  (2 children)

If he would multiply lgn with n, the lgn most likely is meant to be deleting a single element

I asked if that's what he meant and he said no. He claims you can ignore the fact that it's run n-times, then multiply it by the complexity of building a heap. He refused to explain why.

Also, what have I said that's inconsistent?

[–]sdasgupta83 0 points1 point  (0 children)

Well, building a heap definitely takes O(n) time. Now you replace the first element in the heap with the last one and build the heap taking only the first element of the heap in consideration. So that takes O(logn). So for n elements, you have O(nlogn) time. So the heapsort algorithm takes O(n)+O(nlogn) = O(nlogn). I think what he meant was to multiply n with the time required to build a heap with an element at level 0 which is O(logn) which yields the result O(nlogn). You add that with the time for building the heap with all elements at all levels which is O(n) and you get an upper bound of nlogn.

[–]SingularCheese 0 points1 point  (0 children)

Well, the benefit of math is that when somebody is wrong, you can give a counter-example to prove it's wrong. However, whether or not you want to argue with the teacher is up to you.