Re: Sorting algorithms
Pascal J. Bourguignon <pjb <at> informatimago.com>
2010-09-03 03:34:21 GMT
Gustavo <gugamilare <at> gmail.com> writes:
> According to tests I've run in my machine, STABLE-SORT is actually
> faster than SORT. The reason seems to be that STABLE-SORT uses merge
> sort and SORT uses heapsort. The problem with heapsort (according to
> wikipedia) is that it does not access the elements of the array in
> sequential order, so merge sort plays better with modern computer.
>
> I've run various tests with randomly-generated vectors, the code is
> in the (hackish) file sort.lisp attached and the output is in
> sort.log. The only tests where SORT is faster as STABLE-SORT is when
> all elements of the vector were the same. In other tests,
> STABLE-SORT is a lot faster than SORT (often twice as fast).
>
> Of course, this is just in my system, but, if STABLE-SORT is faster
> than SORT, it makes more sense to always use STABLE-SORT and comment
> away SORT's code.
Yes, and bubble sort is faster when the vector is already sorted.
> My system is an Athlon 64 X2 4200+ with 2GiB of DDR2 800MHZ RAM.
>
> As part of a project in a discipline at college, I intend to write
> some sorting algorithms and compare them performance-wise in various
> cases, specially merge sort, heapsort, introsort and a modification
> of quicksort with a selection algorithm (which is also O(n *
> log(n)). Unless someone has an objection, I can port the "winner" to
> SBCL after verifying it is better than current algorithms in SBCL.
The problem is that it would have to be better on all the target,
whatever the processor, cache architecture and memory size, and
whatever the size of the sequence being sorted, and its amount of
"unsortedness".
The point is that there won't be a unique best algorithm.
For a generic library sort such as CL:SORT and CL:STABLE-SORT,
selecting an algorithm that's good enough in general is all that is
needed.
Then you could provide all the other algorithms, well optimized, in a
library, to let the application developers needing a little speed, to
choose a sorting algorithm better adapted to his circumstances,
processor and data.
--
--
__Pascal Bourguignon__ http://www.informatimago.com/
------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:
Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd