#!/usr/bin/env ruby # 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. # bubble_sort - sort, in place, the list given by 'entries' # As a convenience, the list 'entries' is also returned by this function def bubble_sort(entries) (entries.length-1).downto(1) do |i| i.times do |j| if entries[j+1] < entries[j] entries[j+1],entries[j]=entries[j],entries[j+1] end end end; entries end if ARGV.length != 1 $stderr.puts "Usage:\n\t#{$0} (file with numbers to sort)" exit -1 end # read in the entries entries = Array.new IO.foreach(ARGV[0]) { |line| entries.push Float(line) } # sort them bubble_sort(entries) # display the results entries.each { |line| puts line }