Mail Archives: djgpp/1998/02/23/03:00:37
STEVEN S. FALLS wrote:
>
> What is the best sorting algorythm and what is it. I mean, how would
> one program the algrothym?
There is no thing as _the_best_ sorting algorithm. It depends
on the number of items you want to sort, how the data is
pre-sorted, if there are memory constraints etc.
One of the best, on average, is quicksort, which takes
O(n log(n)) operations (from the back of my head, no
gurantees). It can be a bit quirky when the collection is nearly
sorted, or sorted in reverse order. See the implementation
in libc for an implementation that is modified to avoid some
of the pitfalls.
A simpler one is bubblesort, where you plough through the list,
comparing an element with the next one. If they are out of
order, the get swapped. This is repeated until no more
swappings occur in one run.
IIRC, Sedgwick's "Alogrithms in C" has a few examples.
--
Ciao
Tom
*************************************************************
* Thomas Demmer *
* Lehrstuhl fuer Stroemungsmechanik *
* Ruhr-Uni-Bochum *
* Universitaetsstr. 150 *
* D-44780 Bochum *
* Tel: +49 234 700 6434 *
* Fax: +49 234 709 4162 *
* http://www.lstm.ruhr-uni-bochum.de/~demmer *
*************************************************************
- Raw text -