Insertion Sort in Matlab

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

Insertion Sort in Matlab

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

function sorted = insertion_sort(array)
% Insertion sort in ascending order

    error('Input must be a 1xN vector');
    error('Input should not be empty');
disp(['Array to be sorted: ' num2str(array)]);
n = length(array);
for i = 2:n
    d = i;    
    while((d > 1) && (array(d) < array(d-1)))
        temp = array(d);
        array(d) = array(d-1);
        array(d-1) = temp;
        d = d-1;
sorted = array;
disp(['Sorted Array: ' num2str(array)]);

input_array = [9 8 7 6 5 4];
%input_array element can be different
output_array = insertion_sort(input_array);

Insertion Sort in Matlab was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.