function x = insertionSortSimple(x)
% Sort elements of vector x in ascending order.
for k = 2:length(x)
x(1:k) = insertLast(x(1:k));
end

function x = insertLast(x)
% Moves last element of x to its sorted position.
% Assumes that all other elements of x are sorted in ascending order.
% Returns x fully sorted.
k = length(x) - 1;
while k > 0 && x(k+1) < x(k)
% Swap x(x+1) and x(k)
tmp = x(k+1);
x(k+1) = x(k);
x(k) = tmp;

k = k - 1;
end