Pages

Subscribe:

Blogger templates

Monday, February 4, 2013

MERGE SORT



//MERGE SORT
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct salesman
{
char name[40];
float salary;
int experience;
};

void main()
{
clrscr();
salesman sale[3];
for(int i=0;i<3;i++)
{
cout<<"\nEnter the data for "<<i+1<<" salesman records:";
cout<<"\n\nEnter the name of the salesman-";
gets(sale[i].name);
cout<<"\nEnter the experience of the salesman-";
cin>>sale[i].experience;
cout<<"\nEnter the basic salary of the salesman-";
cin>>sale[i].salary;
}
int choice;
char ch='y';
while(ch=='y')
{
cout<<"\n\nPick the options below according to which you want to sort the salesman criteria:";
cout<<"\n1.According to their experience";
cout<<"\n2.According to their basic salary";
cout<<"\nEnter your choice";
cin>>choice;
int j,temp;
salesman temp2;
switch(choice)
{
case 1:cout<<"\n1.According to their experience";
for(i=1;i<3;i++)
{
temp=sale[i].experience;
j=i-1;
while(temp<sale[j].experience && j>=0)
{
temp2=sale[j];
sale[j]=sale[j+1];
sale[j+1]=temp2;
j=j-1;
}
}
cout<<"\nThe sorted list is:";
for(i=0;i<3;i++)
{
cout<<"\nSalesman name is:"<<sale[i].name;
cout<<"\nSalesman experience is:"<<sale[i].experience;
cout<<"\nSalesman basic salary is:"<<sale[i].salary;
}
break;
case 2:cout<<"\n2.According to their basic salary";
for(i=0;i<3;i++)
{
temp=sale[i].salary;
j=i-1;
while(temp<sale[i].salary && j>=0)
{
temp2=sale[j];
sale[j]=sale[j+1];
sale[j+1]=temp2;
j=j-1;
}
}
cout<<"\nThe sorted list is:";
for(i=0;i<3;i++)
{
cout<<"\nSalesman name is:"<<sale[i].name;
cout<<"\nSalesman experience is:"<<sale[i].experience;
cout<<"\nSalesman basic salary is:"<<sale[i].salary;
}
break;
case 3: cout<<"\nWRONG CHOICE!!!";
break;
}
cout<<"\nDo you want to choose again";
cout<<"\nEnter your choice (y/n)";
cin>>ch;
}
getch();
}

0 comments:

Post a Comment