Friday, 7 July 2017

C++ program of insertion sort

//Program starts

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int n=5,a[n],j,key;
for(int i=0;i<n;i++)
{
cout<<"Enter data at ["<<i<<"] : ";
cin>>a[i];
}

for(int i=1;i<n;i++)
{
key=a[i];
j=i-1;

while(a[j]>key && j>=0)
{
a[j+1]=a[j];
j--;
}

a[j+1]=key;
}
cout<<"\nData in sorted form \n \n";
for(int i=0;i<n;i++)
{
cout<<"Data at ["<<i<<"] : "<<a[i]<<endl;

}

}
//Program ends

OUTPUT:

Share:

0 comments:

Post a Comment