#!/usr/bin/env python # 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. import sys # insertion_sort - sort, in place, the list given by 'entries'. # As a convenience, the list 'entries' is also returned by this function def insertion_sort(entries): for i in xrange(1,len(entries)): value = entries[i] for j in xrange(i-1, -2, -1): if entries[j] <= value: break entries[j+1] = entries[j] entries[j+1] = value return entries if len(sys.argv) != 2: print >>sys.stderr,"Usage\n\t%s (file with numbers to sort)" % sys.argv[0] sys.exit(-1) # read in the entries entries=[]; fh=open(sys.argv[1]); for entry in fh: entries.append(float(entry)) # sort them & display the results for entry in insertion_sort(entries): print entry