% Insert a value into an array sorted least->greatest; A = [ 50 10 5 2 1 0]; %Initialize Array x = input('Enter the New Value'); disp('Old Array is '); disp(A); current = 1 ; % Initialize array index last = size(A,2); %Get number of elements in array % Find the index where x belongs! while(current<=last) if x < A(current) current = current + 1; else break; end end % Move all the remaining elements up 1 if (current < last) % Need to move some up for index = last:1:current A(index+1) = A(index); end end A(current) = x; disp('New Array is'); disp(A);