Perl Monk, Perl Meditation | |
PerlMonks |
Simple bubble sortby IndyZ (Friar) |
on Sep 19, 2001 at 08:23 UTC ( [id://113259]=CUFP: print w/replies, xml ) | Need Help?? |
Ah, the bubble sort. Every computer science student loves it for it's simplicity, and hates it for it's unbelievable innefficiency. This example of a bubble sort takes an array ref and sorts the array in-place, from low to high. The array should be only numbers, no strings or other sneaky stuff.
Usage: bbl_sort(\@array); For the curious, sorting a 10,000 element array of unique numbers on my 1.2 Ghz Athlon takes about 3 minutes on a randomized array. The worst-case-scenario, with all of the numbers backwards (i.e., 40, 39, ..., 0), takes a few minutes more. About an hour ago I set the computer to task on a 100,000 element array of unique numbers that had been randomly mixed up, and it is still going.
Back to
Cool Uses for Perl
|
|