#!/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. function entries=selection_sort(entries) for i=1:size(entries,1), % find minimum value in the range i..(size(entries,1)) min_idx=i; for j=i+1:size(entries,1), if entries(j,1) < entries(min_idx,1), min_idx=j; endif endfor % if needed, swap i-th entry with min_idx entry if min_idx != i, swap_tmp = entries(i, 1); entries(i, 1) = entries(min_idx, 1); entries(min_idx, 1) = swap_tmp; endif endfor endfunction if nargin != 1, fprintf(stderr, "Usage:\n\t%s (file with numbers to sort)\n", program_name); exit(-1); endif file_name = argv{1}; fh = fopen(file_name); if fh == -1, fprintf(stderr, "Can't open \"%s\"\n", file_name); exit(-1); endif % read in entries entries = fscanf(fh, "%f\n"); fclose(fh); % sort them entries = selection_sort(entries); % display the results for i=1:size(entries,1), disp(entries(i,1)); endfor