Perl Monk, Perl Meditation | |
PerlMonks |
Re: sort of misunderstanding of sortby ikegami (Patriarch) |
on Mar 21, 2023 at 20:57 UTC ( [id://11151107]=note: print w/replies, xml ) | Need Help?? |
[Woops, this doesn't answer the question.] Checking every single pairing would be wasteful. If we know that a < b and that b < c, then we know that a < c. No need to check that one. There are algorithms that effectively check every pairing in some circumstances, including Bubble Sort and so-called Quicksort. Perl used to use Quicksort because it can be fast outside of the degenerate cases. But the degenerate case happens to be an already-sorted list, which is a common thing to sort. Perl now uses Merge Sort, a much better algorithm. It always performs n log2 n comparisons for an input of size n, which is far less than the n2 approach of checking every pairing. (And it's also stable, meaning that items that compare equally will maintain their relative order. This is a very useful property.) I linked to the Wikipedia pages of algorithms I mentioned, and they are great. They even include useful animations of the sorting algorithm in action.
In Section
Meditations
|
|