14. Write a menu driven program to implement the following sorting and searching algorithms:
a) Insertion Sort
b) Binary Search
c) Bubble Sort
d) Selection Sort
---------------------------------------------------------------------------------------------------------------------
#include<iostream>
#include<string.h>
#include<process.h>
#include<dos.h>
using namespace std;
int k[100]={0};
class array
{
int beg,last,n1;
public:
array(int n);
void insertion();
int binary(int n);
void selection();
void bubble();
void display();
}; array::array(int n)
{
//ar[20][20]={0};
beg=0;
last=n-1;
n1=n;
}
void array::insertion()
{
int i,j,temp;
for(i=1;i<n1;i++)
{
j=i;
while((j>0)&&(k[j-1]>k[j]))
{
temp=k[j-1];
k[j-1]=k[j];
k[j]=temp;
j--;
}
}
}
int array::binary(int n)
{
int mid=(beg+last)/2;
if(beg<=last)
{ if(n>k[mid])
beg=mid+1;
else if(n<k[mid])
last=mid-1;
else
{
cout<<"found:";
return k[mid];
}
binary(n);
}
else
return -1;
}
void array::selection()
{
int small,temp;
for(int i=0;i<n1;i++)
{
small=k[i];
temp=i;
for(int j=i;j<n1;j++)
{
if(small>k[j])
{
small=k[j];
temp=j;
}
}
k[temp]=k[i];
k[i]=small;
}
}
void array::bubble()
{
for(int i=1;i<n1;i++)
{
for(int j=0;j<n1-i;j++)
{
if(k[j]>k[j+1])
{
int temp=k[j];
k[j]=k[j+1];
k[j+1]=temp;
}
}
}
}
void array::display()
{
for(int i=0;i<n1;i++)
cout<<k[i]<<" ";
cout<<endl;
}
int main()
{
char ch;
int n,cho,flag=0;
cout<<"How many elements you want to enter:\n";
cin>>n;
array a1(n);
do
{
cout<<"enter the sample array:\n";
for(int i=0;i<n;i++)
cin>>k[i];
cout<<"What do you want to do:\n1.inserstion sort\n2.Bubble sort\n3.Selection Sort\n4.Binary Search\n5.to exit\n";
cin>>cho;
switch(cho)
{
case 1: a1.insertion();
a1.display();
break;
case 2: a1.bubble();
a1.display();
break;
case 3: a1.selection();
a1.display();
break;
case 4: cout<<"Enter the elemnt to be searched:\n";
cin>>n;
a1.bubble();
n=a1.binary(n);
if(n!=-1)
cout<<"The element "<<n<<" got found:\n";
break;
case 5: cout<<"Exiting....\n\n\n";
for(int i=0;i<2000000;i++){flag=1;}
break;
//exit(0);
default:cout<<"Enter choice only between 1-5:\n";
}
if(flag==0)
{
cout<<"\n\n\nWant to enter more:\n";
cin>>ch; }
else
ch='n';
}while((ch=='y')||(ch=='Y'));
}
--------------------------------------------------------------------------------------------------------------------
Programs Finish............