user3504876
user3504876

Reputation: 49

Why isn't heapsort lg(n!)?

I'm reading CLRS and it says heapsort is

HEAPSORT(A):
BUILD-MAX-HEAP(A);
for (i = A.length; i >= 1; i++)
{
    exchange A[1] with A[i];
    A.heap-size = A.heap-size - 1; 
    MAX-HEAPIFY(A,1);
}

MAX_HEAPIFY is O(lg n). The book says it runs MAX-HEAPIFY n times thus it is O(n lg n) time.

But if the heap is shrinking in size by 1 each iteration shouldn't it be O(lg n!) ?

It would be lg 1 + lg 2 ... + lg(n-1) + lg (n) = lg(n!), right ?

Upvotes: 4

Views: 217

Answers (1)

chrk
chrk

Reputation: 4215

Actually it's Stirling's Approximation:

O( log(n!) ) = O(nlogn)

Upvotes: 10

Related Questions