In article 3ed@vixen.cso.uiuc.edu, wemccaug@prairienet.org (Wendy E. McCaughrin) writes:
>
>>
>>Is it really 2logn? My understanding was that a sort couldn't be
>>less than nlogn... More info, please.
>>
> You are correct: Quicksort performance is O(n*log(n)), usually
...but this is only the best - and average-case!
In the worst case Quicksort needs also O(N^2) steps (comparements) like the Insertion Sort.
The exact recursive formula for the average-case performance is
T(N) = c*(N + (1/N)*Sum( T(n) + T(n-k) , k=1..N) ) (it's really the upper bound, so you can replace = by <, and c is a machine-dependent constant, can be set to 1)
and for the best case:
T(N) = c*(N + 2*T(N/2 ) ) (which can be easily resolved as c*N*log(N) )
while the worst case formula sounds:
T(N) = c*(N + T(N-1) ) ( because sequences of single element are ignored,)
which is simply T(N) = c*T(N-1) + c*N = c*T(N-2) + c*(N-1) + c*N = ... =