google search

Popular Posts


Welcome to BCA Notes

>>>>>>>>>>>>>>>>

Visitors

Search This Blog

Blogger templates

Visitor Map


Thursday 21 August 2014

Insertion sort algorithm

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],i,n,p,ptr,temp;

cout<<"\n------------ INSERTION SORT ------------ \n\n";

cout<<"Enter No. of Elements : ";
cin>>n;

cout<<"\nEnter Elements : \n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}

a[0]=0;

for(p=2;p<=n;p++)
{
temp=a[p];
ptr=p-1;

while(temp<a[ptr])
{
a[ptr+1]=a[ptr];                // Move Element Forward
ptr--;
}

a[ptr+1]=temp;                 // Insert Element in Proper Place
}

cout<<"\nAfter Sorting : \n";
for(i=0;i<n;i++)
a[i] = a[i+1];
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}

getch();
}




0 comments:

Post a Comment