#!/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. # insertion_sort - sorts the array referenced by the first argument to this # function sub insertion_sort($) { my $ref_entries = shift; my ($i, $j, $val); for ($i = 1; $i < @$ref_entries; $i++) { $val = $$ref_entries[$i]; for ($j = $i-1; $j > -1; $j--) { last if ($$ref_entries[$j] <= $val); $$ref_entries[$j+1] = $$ref_entries[$j]; } $$ref_entries[$j+1] = $val; } } 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 insertion_sort(\@entries); # display them foreach $entry (@entries) { print $entry; }