selection sort / insertion sort

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();

int a[5],i,j,p,min,t,x,k;
cout<<”enter the numbers:”;

for(i=0;i<5;i++)
{
cin>>a[i];
}

cout<<”enter the type of sorting u wanna do:\n”;
cout<<”1:insertion sort”<<”\n”<<”2:selection sort”;
cin>>t;
switch (t)
{
case 1:
cout<<”sorting by INSERTION sort…” ;

for(i=1;i<5;i++)
{
if(min>a[i])
{
min=a[i];
p=i;
}
}
a[p]=a[0];
a[0]=min ;
for(i=2;i<5;i++)
{
x=a[i];
j=1;
while(x<a[j-1])
{
a[j]=a[j-1];
j=j-1;
}
a[j]=x;
}cout<<”\n sorted list is”;
for(i=0;i<5;i++)
{
cout<<” “<<a[i]<<” “;
}
break;

case 2:
cout<<”SELECTION SORTING…”;
for(i=0;i<4;i++)
{
for(j=i+1;j<5;j++)
{
if(a[j]<a[i])
{ k=a[j];
a[j]=a[i];
a[i]=k;
}
}
}
cout<<”\n sorted list is: \n” ;
for(i=0;i<5;i++)
{
cout<<a[i]<<” “;
}
break;
default : cout<<”wrong choice”;
break;
}
getch();
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.