Published on 28 April 2022 (Updated: 25 September 2022)

Welcome to the Insertion Sort in Matlab page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

functionsorted=insertion_sort(array)% Insertion sort in ascending orderif(size(array,1)>1)error('Input must be a 1xN vector');endif(isempty(array))error('Input should not be empty');enddisp(['Array to be sorted: 'num2str(array)]);n=length(array);fori=2:nd=i;while((d>1)&&(array(d)<array(d-1)))temp=array(d);array(d)=array(d-1);array(d-1)=temp;d=d-1;endendsorted=array;disp(['Sorted Array: 'num2str(array)]);endinput_array=[987654];%input_array element can be differentoutput_array=insertion_sort(input_array);