MATLAB: Help with sort function

arrayMATLABsort

I am writing my own sorting function, I've gotten the minimum value for the array and set that to the other array, and I couldn't figure out how to get rid of that minimum value in the original array and tell the program to find the next smallest minimum value. Here's my code so far
clear,clc;
x = [6 5 3 5 9 10 438 4 1 4 7 0 4 8 4 2];
len = length(x);
minval = x(1);
for n = 1:len
for i = 1:len
if x(i) < minval
minval = x(i);
end
end
sortval(n) = minval;
end
disp(sortval)
I was thinking maybe I could delete that value, but doing that would change the size of the array and it wouldn't work.

Best Answer

You can sort the vector "inplace": Move the minimum value to front and start the next iteration at the next element:
x = [6 5 3 5 9 10 438 4 1 4 7 0 4 8 4 2];
len = length(x);
for n = 1:len
minind = n;
minval = x(n);
for i = n+1:len % Start at n+1
if x(i) < minval
minval = x(i);
minind = i; % Remember index
end
end
tmp = x(n); % Swap current with minimal element
x(n) = minval;
x(minind) = tmp;
end
Finally x contains the sorted array and the processing time is reduced, because the inner loop is shortend. (By the way: You can omit the last iteration)
Have fun with developping sorting algorithms. This is an interesting topic and it is worth to dig in WikiPedia.