Mail Archives: djgpp/1998/01/23/12:43:47
Charles Krug wrote:
>
> If you look at quicksort, you see log(n)
> outer loops, each containing (n-1) inner loops, giving a complexity of
> log(n)(n-1) or nlog(n) - log(n). Which has a complexity of nlog(n).
>
Nitpicking a little here: Quicksort is not an nlog(n) algorithm, but an
n^2 one, in its worst case. However, this worst case is very rare (for
large values of n), and a correct implementation may make it very
unlikely in all practical cases.
There are nlog(n) sorting algorithms though, one of them being Heapsort.
But Quicksort happens to be faster on average (though its "nominal
complexity" is higher).
This is an important point when choosing an algorithm. An nlog(n)
algorithm transforms some data of length n in Knlog(n) steps, K being a
constant. The value of K may have a drastic effect on the actual speed of
the program. In fact, in several cases, an o(nlog(n)) algorithm with low
K may be preferable (for some values of n) to an o(n) one with a higher
constant (as an example, the Fast Fourier Transform, an o(nlogn)
algorithm, is usually faster, for standard values of n, the Fast Wavelet
Transform algorithm, which is o(n)).
Francois
- Raw text -