Pages

E-mail us at prgms4starters@gmail.com for any queries or to share your programs. Please mention the program definition in the subject line.

In case you need help in finishing a program, do mention "incomplete pgm" in the subject line. Our team of experts will do their best to help you.

Click the link to follow this page for updates and reminders: FOLLOW THIS SITE

You can also visit our Facebook page by clicking HERE

**don't forget to participate in the polls at the bottom of the page

Friday, November 5, 2010

Binary and Linear Search

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
int binarysearch(int[],int,int);
void sort(int[],int);
int count;

int linear(int [],int,int);
int iar[20],isize,ioptn,item,cou=0,ivalu;
void main()
{

clrscr();
int ar[50],item,n,index,b=1,ic,w=1;
while(w==1)
{
 cout<<"\nSearching techinques";
 cout<<"\n______________________";
 cout<<"\n1.Binary seaching:";
 cout<<"\n2.Linear searching:";
 cout<<"\n3.Exit:";
 cout<<"\n Enter the choice:";
 cin>>ic;
 switch(ic)
 {
 case 1:
 cout<<"\n_____________________";
 cout<<"\nEnter the no of elements: ";
 cin>>n;
 cout<<"\nEnter the elements:\n ";
 for(int i=0;i<n; i++)
  {
 cin>>ar[i];
  }
 while(b==1)
 {
  sort(ar,n);
  cout<<"\nThe sorted array is: \n";
  for( i=0;i<n;i++)
  {
 cout<<ar[i]<<" ";

  }
 cout<<"\nEnter the item to be searched: ";
 cin>>item;
 index=binarysearch(ar,n,item);
 if(index==-1)
 cout<<"\ninvalid item: ";
 else
 cout<<"\nElement found at position: "<<index+1;
 cout<<"\nTotal no of comparisons= "<<count;

 cout<<"\nPress 1 to continue or any other digits to exit : ";
 cin>>b;
 }
 break;
case 2:
clrscr();
cout<<"\n  SEARCHING TECHIQUES";
cout<<"\n ______________________";
cout<<"\n Linear searching:";
cout<<"\nEnter the array size :";
cin>>isize;
cout<<"Enter the Elements of the array :";
for(i=0;i<isize;i++)
{
cin>>iar[i];
cout<<"\t";
}
while(n==1)
{
cout<<"\nEnter the item to search :";
cin>>item;
ivalu=linear(iar,isize,item);
cout<<"\n The array : ";
for(int j=0;j<isize;j++)
{
 cout<<iar[j];
 cout<<"\t";
}
if(ivalu==-1)
cout<<"\nThe element was not found:";
else
{
cout<<"\n The element found ";
cout<<"\n The element :"<<item;
cout<<"\n The postion of the item is:"<<ivalu+1;
cout<<"\n Total number of searching:"<<cou;
}
cout<<"\nPress 1 to continue or any other no to exit :";
cin>>n;
 }
break;
case 3:exit(0);
       break;
}
}
cout<<"\nPress 1 to continue or any other no to exit :";
cin>>w;
getch();
}

void sort(int ar[],int size)
{
int s,p,temp;
for(int i=0;i<size;i++)
{
s=ar[i];
p=i;
for(int j=i+1;j<size;j++)
{
if( ar[j]<s)
{
s=ar[j];
p=j;
}
}
temp=ar[i];
ar[i]=ar[p];
ar[p]=temp;

}

}

int binarysearch(int ar[],int size,int item)
{


int low,high,mid;
low=0;
high=size-1;
while(low<=high)
{
count++;
mid=(low+high)/2;
if(item==ar[mid])
return mid;
else if(item>ar[mid])
low=mid+1;
else
high=mid-1;
}
return -1;
}

int linear(int iar[],int isize,int item)
{
  for(int i=0;i<isize-1;i++)
  {
  cou++;
  if(iar[i]==item)
   return i;

  }
  return -1;

}

2 comments:

anoop said...

dude please include the pgm using recursive method too /////////////

eapzz said...

dude.... within a short span of time v wl upload surely!! d time delay is bcoz v r havng xams right nw!! anyways sry 4 d delay!!

Post a Comment

We have noticed that 20% of our visitors are still using internet explorer. For a better and faster surfing experience we recommend that you switch to either Google Chrome or Mozilla Firefox. We cannot provide you the downloads but here are the links to sites where you can download them from

Google Chrome - click here to go to site

Mozilla Firefox - click here to go to site

Where are you from

Do you think this site was helpful

Should we put up more C or C++ programs