% This program provides a list of indices that sorts the array x. The sorting
% algorithm is very infefficient, O(N^2).
function sort_index = cksort(x)

% NOTE: indenting means nothing to octave, but makes the code more readable.

N = numel(x);  % number of elements in x.
for i=1:N
   irank = 1;  % initial value of the ith element rank. 1 means smallest.
   for j=1:N
      if ( x(j) < x(i) )  % each value of x that is smaller than x(i)...
         irank ++;        % ...implies the rank of x(i) should be increased.
      endif 
   endfor
   sort_index(irank) = i; % i.e.: "The irank-th smallest value of x is x(i)."
endfor
% Now x(irank) is a sorted version of the array x!