Why not heapsort lg (n!)?

I read CLRS and he says heapsort

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 - O(lg n). The book says that it launches MAX-HEAPIFY ntimes, so this is the time O(n lg n).

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

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

+4
source share
1 answer

This is actually Stirling's Approximation :

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

+10
source

Source: https://habr.com/ru/post/1535293/


All Articles