Octave solution
function index = first_missing(array)
for i = 1:numel(array)
index = array(i);
if index > 0 && index <= numel(array)
array(i) = array(index);
array(index) = index; end; end;
index = i + 1;
while i > 0
if array(i) != i
index = i; end;
i--; end; end;