Thursday 17 January 2013

Function ( Binary and Linear Search)

| |


#include<iostream>

using namespace std;

int main()
{
int linearsearch();
int binarysearch();
cout<<"The number found via linear search\n"<<linearsearch();
cout<<"The number found via Binary search\n" <<binarysearch();

system ("pause");}
int linearsearch()
{int sc[100],n,count=0,y;
cout<<"How many numbers u want to enter?";
cin>>y;
cout<<"Enter the numbers";

for(int i=0; i<y; i++)
{cin>>sc[i];}
cout<<"Enter a number for the query! ";
cin>>n;
for(int j=0; j<y;j++)
{if(sc[j]==n)
{
count=count+1;
}}
if(count==1)
{
            cout<<"The requred query "<<n<<" is found! \n";
            }
else
{
cout<<"The required query cannot be found! \n";}
    return y; }
   
   int binarysearch()
   {int a[10],n,beg,end,mid,x;
cout<<"\nNo of element in array\t";
cin>>n;
cout<<"enter the element\n";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\nenter item to be searched\t";
cin>>x;
beg=0;end=n;
mid=(beg+end)/2;
while(beg<=end&&a[mid]!=x)
{
if(x<a[mid])
{
end=mid-1;
}
else
{
beg=mid+1;
}
mid=(beg+end)/2;
}
if(a[mid]==x)
{
cout<<"\nItem found at location "<<mid+1;
}
else
{
cout<<"\nItem not found";
}
   return x;}
   


Function ( Binary and Linear Search)


Related Posts Plugin for WordPress, Blogger...
Powered by Blogger.