Tuesday, July 12, 2016

Insertion-sort

Insertion Sort Matlab Program

By studying this, one can understand the logic of insertion sort. Insertion sort is very simple algorithm and easy to implement. It works well when the input size is less. If the input size is more, insertion sort is not efficient(quick sort or merge sort works good when the input size is big).

% Program for Insertion sort 
% Author : M Naveenkumar , Research Scholar , NIT Trichy, Tamilnadu, India
% n = input size (array size)

clear all; clc;
n = input('Enter n value:');

% create an array (a) with size 'n' and filled with zeros
a(1,n)=0;

% reading 'n' values to array (a)
for i=1:n
    string = 'value:';
    a(1,i) = input(string);
end

disp ('_______________________________________________________________');

for j=2:n
    key = a(1,j);
    
    % insert 'key' into already sorted array a[1.... j-1]
    i = j-1;
    while (i>0 && a(1,i)> key) 
        a(1,i+1) = a(i);
        i = i-1;
    end 
    a(1,i+1) = key;
    
    disp ('.................................................');
    fprintf('At j =%d:\n',j);
    disp(a);
end

disp ('_______________________________________________________________');
disp('sorted elements:');
disp(a);

Output :


Enter n value:6
value:2
value:1
value:9
value:5
value:7
value:8
_______________________________________________________________
.................................................
At j =2:
     1     2     9     5     7     8

.................................................
At j =3:
     1     2     9     5     7     8

.................................................
At j =4:
     1     2     5     9     7     8

.................................................
At j =5:
     1     2     5     7     9     8

.................................................
At j =6:
     1     2     5     7     8     9

_______________________________________________________________
sorted elements:
     1     2     5     7     8     9


Popular Articles:

1. matlab-cropping-binary-image-algorithm

Objective of the Program: Program takes a black and white image as input. It removes the black portion and gives the white portion of the image.

2. Working-with-ROI-of-image-using-Matlab

Objective of the Program:The part of the image, on which you have interest to work out, is called Region of Interest (ROI). In another words, selected subset of image is called ROI. In some contexts, you want to apply operations on ROI of image rather than the entire image. To achieve this, generally people extract the ROI from the image, store it in another variable and then apply operations on ROI. If you want to apply your operations on ROI without extracting from the image, it is bit difficult. This article will explain the performing the operations on ROI without extracting from the image. In this context, the ROI part of image is affected rather than the entire image.

No comments:

Post a Comment

Python-environment-for-deep-learning-in-windows

Python is increasingly becoming a popular programming language for machine learning and deep learning. If you want to use python for train...