You are on page 1of 3

Roll.

No:- 5608017 Date:-

Aim:- Write a program to implement insertion sort.

Algorithms:- Insertion – Sort[A)].

1. For j  2 to length[A].
2. Do key  A[j].
3. Insert A[j] into stored sequence A[1_ _ _j-1].
4. I  j-1.
5. While i>0 and A[i]; key.
6. Do A[i+1]  A[i].
7. I  i-1.
8. A[i+1]  key.
Coding:-

#include<stdio.h>
#include<conio.h>
void main ( )
{
int a[100],n,k,I,j,temp;
clrscr ( );
printf(“enter the no, of elements”);
scanf (“%d”,& n);
printf(“%d”,& n);
printf(“\n enter the elements”);
for(i=0;i<n;i++)
{
scanf(“%d”,& a[i]);
}
for(k=1;k<=n;k++)
{
temp =a[k]
j =k-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf(“\n elements of array after sorting are”);
for(i=0;i<n;i++)
{
printf(“%d\t”;a[i];
}
getch( );
}
Output:-
Enter the no. elements =7.
Enter the elements = 12 56 34 21 67 89 52.
Elements of array after sorting are = 12 21 34 56 57 67 89.

You might also like