#!/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 def selection_sort(entries): for i in xrange(0, len(entries)): # find minimum value in the range i..(len(entries)-1) min_idx = i for j in xrange(i+1, len(entries)): if entries[j] < entries[min_idx]: min_idx = j # if needed, swap the i-th entry with the min_idx entry if min_idx == i: continue (entries[i], entries[min_idx]) = (entries[min_idx], entries[i]) 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 entries entries = [] fh = open(sys.argv[1]) for line in fh: entries.append(float(line)) fh.close() # sort them selection_sort(entries) # display the results for entry in entries: print entry