A program to initialize a 1-D array and sort it in ascending order using Insertion Sort method.

 

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a[10],i,j,min;

cout<<"Enter elements of the array:"<<endl;
for(i=0;i<10;i++)
{
cout<<"Enter element no."<<i+1<<": ";
cin>>a[i];
}

for(i=1;i<10;i++)
{
min=a[i];
j=i-1;
while(min<a[j] && j>=0)
{
a[j+1]=a[j];
j--;
}
a[j+1]=min;
}

cout<<"\nElements of the sorted array:"<<endl;
for(i=0;i<10;i++)
cout<<a[i]<<" ";

gotoxy(25,24); cout<<"Press any key to continue......";
getch();
}

 

< Back