summaryrefslogtreecommitdiff
path: root/3808/CH3/EX3.5/Ex3_5.sce
blob: 87528b20a0c0e1e207470557310971a69ec96e3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Chapter 03: Algorithms

clc;
clear;

function result = insertionSort(Arr)
    for i=2:length(Arr)
        A = Arr(i);
        j = i-1;
        while (j>0 & Arr(j) > A)
            Arr(j+1) = Arr(j);
            j = j-1;
        end
        Arr(j+1) = A;
    end

result = Arr;
endfunction

arr=[3 2 4 1 5]
disp(arr,"Given Array")
arr_s=insertionSort(arr)
disp(arr_s,"Sorted Array")