//Program to sort an array using Insertion sorting.
#include<iostream.h>
#include<conio.h>
void insert(int* ,int );
void main()
{
clrscr();
int A[10],i,N;
cout<<"\nEnter number of elements in array : ";
cin>>N;
cout<<"\nElements are : " ;
for(i=0;i<N;i++)
cin>>A[i];
insert(A,N);
cout<<"\nSorted array is : ";
for(i=0;i<N;i++)
cout<<" "<<A[i];
getch();
}
void insert(int a[10],int n)
{
for(int i=1;i<=n-1;i++)
{
int item=a[i];
int hole=i;
while(hole>0 && a[hole-1]>item)
{
a[hole]=a[hole-1];
hole=hole-1;
}
a[hole]=item;
}
}
Output:
No comments:
Post a Comment