0
2.9kviews
Write a program to sort an array using insertion sort.

Mumbai University > Information Technology > Sem 3 > Data Structure and Algorithm analysis

Marks: 10 M

Year: May 2015

1 Answer
0
105views

Insertion sort is a popular as well as simple sorting algorithm. Here the array consist of two sections; the Sorted part and the Unsorted part. In each iteration we select the first element from the unsorted list and add it to its rightful position in the sorted list.

Program:

#include<stdio.h>
#include<conio.h>
Void main()
{
Int I,n,a[10];
Printf(“enter the number of elements”);
Scanf(“%d”,&n);
Printf(“\n Eneter the elements”); 
For(i=0;i<n;i++)
{
    Scanf(“%d”,&a[i]);
}
Insertion_sort(a,n);
Printf(“the sorted list is \n”);
For(i=0;i<n;i++)
{
    Printf(“%d”,a[i]);
}

}
Void insertion_sort(int a[10],int n)
{
Int ;
For(i=1; i<=n;i++)
{
    Temp=a[i];
    J=i-1;
    While((temp<a[j])&&(j>=0))
    {
        A[j+1]=a[j];
        j--;
    }
    A[j+1]=temp;
}
}
Please log in to add an answer.