Tuesday, 22 January 2013

Insertion Sort


By on 22:49

Insertion Sorting is the sorting algorithm or a type of sorting which is used to sort data items in ascending or descending order.Best case Complexity of insertion sort is O(n) for comparison and O(1) for swap.Insertion sort is best for used when list size is small.This technique is used in c ++,java,c# language etc.

Insertion Sorting Example and Algorithm:-


=6    5    1    3    2    8    4(unsorted)

=5    6    1    3    2    8    4
=1    5    6    3    2    8    4
=1    3    5    6    2    8    4
=1    2    3    5    6    8    4
=1    2    3    4    5    6    8(Sorted)

Function cum Program:-



#include<iostream.h>
#include<conio.h>
void insert(int a[],int n)
{
int i,j,t;
for(i=0;i<n;i++)
{
t=a[i];
for(j=i-1;j>=0;j--)
{
if(t<a[j])
a[j+1]=a[j];
else
break;
}
a[j+1]=t;
}}


That's all about Insertion Sorting in data structure and computer science.



If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...