#!/usr/bin/octave -qf % 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. % insertion_sort(entries) - sorts the vector 'entries' and returns the results function entries=insertion_sort(entries) for i=2:size(entries,1), value = entries(i); for j=i-1:-1:1, if entries(j) <= value, entries(j+1)=value; break; endif entries(j+1) = entries(j); if j == 1, entries(1)=value; endif endfor endfor endfunction if length(argv) != 1, fprintf(stderr, "Usage:\n\t%s (file with numbers to sort)\n", program_name); exit(-1); endif % read in the results fh = fopen(argv{1}); entries = fscanf(fh, "%f\n"); fclose(fh); % sort them entries=insertion_sort(entries); % display the results disp(entries);