Perl's sort function sits on top of C's native qsort
function which is an implementation of quicksort. O(n*log(n))!
| [reply] |
| [reply] |
Does that mean that pre-sorted lists
are sorted slowly,
or is it being randomized before the actual sort?
| [reply] |
It depends on how qsort is implemented by the C
stdlib library with which perl was compiled. My guess is no,
but it really depends on how your C stdlib was implemented.
It is easy to add a "quicksort worst-case avoider"
by not using a "use the first element as the pivot" and instead
doing something like:
- adding a "sorted list detector"
- Picking the pivot randomly (instead of as the first element)
- Shuffling the list before sorting
- Using another pivot picking technique
Since not everyone knows the internals of quicksort, there
is a worst case performance of O(n^2) with quicksort if the
worst pivot is picked for each iteration (if you don't know what a
pivot is don;t worry.... if you want to know I can explain it. This
worst-case performance can happen if the list is already in
is in sorted order and the pivot is picked by choosing the first
element of the list as the pivot. However, there are
techniques for easily avoiding this pitfall.
| [reply] |