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();
}

BINARY SEACH

BINARY SEARCH


//PROGRAM FOR BINARY SEARCH
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct teacher
{
char name[20];
int tno;
float salary;
}obj[5];
void main()
{
clrscr();
int size;
cout<<"\nEnter the no. of records you want to enter (maximum 5):\n";
cin>>size;
for(int i=0;i<size;i++)
{
cout<<"\nEnter the details of "<<i+1<<" teacher-";
cout<<"\nEnter the name of "<<i+1<<" teacher-";
gets(obj[i].name);
cout<<"Enter the teacher id of the "<<i+1<<" teacher-";
cin>>obj[i].tno;
cout<<"Enter the salary of "<<i+1<<" teacher-";
cin>>obj[i].salary;
}
int no,beg=0,end=size-1;
char ch='y';
while(ch=='y')
{
cout<<"\nEnter the teacher id you want to search-";
cin>>no;
int mid;
while(beg<=end)
{
mid=(beg+end)/2;
if(obj[mid].tno==no)
{
cout<<"\nSEARCH IS SUCCESSFUL!!!";
cout<<"\nElement is found at "<<mid+1<<" position";
cout<<"\nThe details of "<<mid+1<<" teacher is:";
cout<<"\nThe name of "<<mid+1<<" teacher is "<<obj[mid].name;
cout<<"\nThe teacher id of the "<<mid+1<<" teacher is "<<obj[mid].tno;
cout<<"\nThe salary of "<<mid+1<<" teacher is"<<obj[mid].salary;
break;
}
else if(obj[mid].tno<no)
beg=mid+1;
else
end=mid-1;
}
if(beg>end)
cout<<"\nSEARCH IS UNSUCCESSFUL! Element could not be found";

cout<<"\n\nDo you want to search again (y/n):";
cin>>ch;
}
getch();
}