/* 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 BubbleSort { // bubbleSort - sorts, in place, the items in the ArrayList 'entries' public static > void bubbleSort(ArrayList entries) { boolean swapped=true; for (int i = entries.size()-1; i >= 0 && swapped; i--) { swapped = false; for (int j = 0; j < i; j++) { if (entries.get(j+1).compareTo(entries.get(j)) < 0) { T tmp = entries.get(j+1); entries.set(j+1, entries.get(j)); entries.set(j, tmp); swapped = true; } } } } static public void main(String[] args) throws Exception { if (args.length != 1) { System.err.println( "Usage:\n\tjava BubbleSort (file with numbers to sort)"); System.exit(-1); } ArrayList entries = new ArrayList(); // read in the entries BufferedReader reader = new BufferedReader(new FileReader(args[0])); for (String inLine; (inLine = reader.readLine()) != null; ) entries.add(Float.valueOf(inLine)); reader.close(); // sort them bubbleSort(entries); // display the results for (Float entry : entries) System.out.println(entry); } }