#!/usr/bin/env perl # Copyright 2005 Daniel Cer (daniel.cer@cs.colorado.edu) # # This work is licensed under the Creative Commons Attribution-NonCommercial- # ShareAlike License. To view a copy of this license, visit # http://creativecommons.org/licenses/by-nc-sa/2.5/ or send a letter to # Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, # 94105, USA. # bubble_sort - sorts the array referenced by the first argument to this # function. sub bubble_sort($) { my $ref_entries = shift; my ($i, $j, $swapped); for ($i = $#$ref_entries, $swapped=true; $i >= 0 && $swapped; $i--) { for ($j = 0,$swapped=false; $j < $i; $j++) { if ($$ref_entries[$j+1] < $$ref_entries[$j]) { $swapped=true; ($$ref_entries[$j],$$ref_entries[$j+1]) = ($$ref_entries[$j+1],$$ref_entries[$j]); } } } } if ($#ARGV != 0) { print stderr "Usage:\n\t$0 (file with numbers to sort)\n"; exit -1; } # read in the entries $file_name = $ARGV[0]; open fh, $file_name or die "Can't open \"$file_name\"\n"; @entries = ; close fh; # sort them bubble_sort(\@entries); # display the results foreach $entry (@entries) { print $entry; }