/* 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 java.io.FileReader; import java.io.BufferedReader; import java.util.ArrayList; public class InsertionSort { // insertionSort - sorts, in place, the items in the ArrayList 'entries' public static > void insertionSort(ArrayList entries) { for (int i=1; i -1; j--) { T entryJ = entries.get(j); if (entryJ.compareTo(val) <= 0) break; entries.set(j+1, entryJ); } entries.set(j+1, val); } } static public void main(String[] args) throws Exception { if (args.length != 1) { System.err.println( "Usage:\n\tjava InsertionSort (file with numbers to sort)"); System.exit(-1); } String filename = args[0]; // read in the entries ArrayList entries = new ArrayList(); BufferedReader reader = new BufferedReader(new FileReader(filename)); for (String inLine; (inLine = reader.readLine()) != null; ) entries.add(Float.valueOf(inLine)); reader.close(); // sort them insertionSort(entries); // display the results for (Float val : entries) System.out.println(val); } }