Pages

E-mail us at prgms4starters@gmail.com for any queries or to share your programs. Please mention the program definition in the subject line.

In case you need help in finishing a program, do mention "incomplete pgm" in the subject line. Our team of experts will do their best to help you.

Click the link to follow this page for updates and reminders: FOLLOW THIS SITE

You can also visit our Facebook page by clicking HERE

**don't forget to participate in the polls at the bottom of the page

Friday, November 5, 2010

Sorting using insertion and selection

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int a[15],n;
void select()
{
int small,k,j,pos;
for(k=0;k<n;k++)
{
small=a[k];
pos=k;
for(j=k+1;j<n;j++)
{
if(a[j]<small)
{
small=a[j];
pos=j;
}
}
int temp=a[k];
a[k]=a[pos];
a[pos]=temp;
cout<<a[k]<<"\t";
}
}
void insert()
{
int t,l;
for(int i=0;i<n;i++)
{

t=a[i];
l=i-1;
while(t<a[l]&&l>=0)
{
a[l+1]=a[l];
l--;
}
a[l+1]=t;

}
}
void entry()
{
cout<<"\n enter the number of elements to be sorted";
cin>>n;
cout<<"\n enter the array";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
}
void main()
{ clrscr();
int c=1,h;

while(c==1)
{
cout<<"\n welcome to the menu";
cout<<"\n 1. selection sort \n 2.insertion sort";
cout<<"\n enter your choice";
cin>>h;
switch(h)
{
case 1: entry();
cout<<"\n sorted array is ";
select();
break;
case 2: entry();
cout<<"\n sorted array is";
insert();
for(int i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
break;
default:cout<<"\n invalid entry";
}
cout<<"\n press 1 to continue";
cin>>c;
}
}

No comments:

Post a Comment

We have noticed that 20% of our visitors are still using internet explorer. For a better and faster surfing experience we recommend that you switch to either Google Chrome or Mozilla Firefox. We cannot provide you the downloads but here are the links to sites where you can download them from

Google Chrome - click here to go to site

Mozilla Firefox - click here to go to site

Where are you from

Do you think this site was helpful

Should we put up more C or C++ programs