User Tools

Site Tools


gibson:teaching:fall-2012:math445:naivesort.m
function x = naivesort(x);
% sort the elems of x in ascending order

  N = length(x);

  for i = 1:N

    % set xmin to first elem of remaining portion of x
    % set jmin to index of first elem in remaining portion of x
    xmin = x(i);
    jmin = i;

    % look through remaining portion of x, find smallest value
    for j = (i+1):N
      if x(j) < xmin
        xmin = x(j);
        jmin = j;
      end
    end

    % swap the current (ith) elem with the smallest found
    tmp = x(i);
    x(i) = x(jmin);
    x(jmin) = tmp;

  end

end
gibson/teaching/fall-2012/math445/naivesort.m.txt · Last modified: 2012/09/13 06:36 by gibson