Showing posts with label Searching. Show all posts
Showing posts with label Searching. Show all posts

Sunday, August 21, 2011

Program to search an element in array using Binary search in C++.


//Program to search an element in array using Binary search in C++.
#include<iostream.h>
#include<conio.h>
void main()
{
 int i, beg, end, loc, mid, item, data[50], n;
 clrscr();
 cout<<"Enter the number of elements in the array:";
 cin>>n;
 cout<<endl<<" Enter the value of "<<n<<" elements:";
 for(i=0; i<n; i++)
 {
   cin>>data[i];
 }
 beg=0;
 end=n-1;
 mid=int((beg+end)/2);
 cout<<endl<<" Enter the given element";
 cin>>item;
 while(beg<=end && data[mid]!=item)
 {
  if(item<data[mid])
  {
   end=mid-1;
  }
  else
  {
   beg=mid+1;
  }
  mid=int((beg+end)/2);
 }
 if(data[mid]==item)
 {
  loc=mid;
  cout<<endl<<"Element found at location:"<<loc;
 }
 else
 {
  cout<<endl<<"Element is not found";
 }
 getch();
}

Saturday, August 20, 2011

Binary search using Recursion

//Binary search using Recursion
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
main()
{
      int i,*a,n,p;
      void create(int **a,int n);
      void display(int *a,int n);
      int search(int a[],int item,int beg,int end);
      a=NULL;
      printf("Enter the number of elements you want in your array : " );
      scanf("%d",&n);
      create(&a,n);
   
       printf("Enter the number to be search : " );
      scanf("%d",&p);
      i=search(a,p,0,n);
      if(i==-1000)
      printf("\nitem no found");
      else
      printf("\n item is found at position %d",i+1);
     getch();
     display(a,n);
      return 0;
}
void create(int **a,int n)
{
     int i;
     int *s;
     s=(int *)malloc(n*sizeof(int));
     printf("\n Enter the value of array elements " );
     for(i=0;i<n;i++)
     {
                  scanf("%d",&s[i]);
     }
     *a=s;
}
void display(int *a,int n)
{ int i;
      printf("\n array elements " );
     for(i=0;i<n;i++)
     {
                  printf("%d ",a[i]);
     }
}  
int search(int a[],int item,int beg,int end)
{
    int mid=(beg+end)/2;
    if (beg>end)
    return -1000;
    if(a[mid]==item)
     return mid;
    if(a[mid]>item)
     return search(a,item,beg,mid-1);
    else
    return search(a,item,mid+1,end);
}

Linear search using recursion.

//Linear search using recursion.
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
main()
{
      int i,*a,n,p;
      void create(int **a,int n);
      void display(int *a,int n);
      int search(int a[],int item,int beg,int end);
      a=NULL;
      printf("Enter the number of elements you want in your array : " );
      scanf("%d",&n);
      create(&a,n);
      display(a,n);
       printf("Enter the number to be search : " );
      scanf("%d",&p);
      i=search(a,p,0,n);
      if(i==-1000)
      printf("\nitem no found");
      else
      printf("\n item is found at position %d",i+1);
     getch();
      return 0;
}
void create(int **a,int n)
{
     int i;
     int *s;
     s=(int *)malloc(n*sizeof(int));
     printf("\n Enter the value of array elements " );
     for(i=0;i<n;i++)
     {
                  scanf("%d",&s[i]);
     }
     *a=s;
}
void display(int *a,int n)
{ int i;
      printf("\n array elements " );
     for(i=0;i<n;i++)
     {
                  printf("%d ",a[i]);
     }
}  
int search(int a[],int item,int beg,int end)
{
    if (beg>end)
    return -1000;
    if(a[beg]==item)
     return beg;
     return search(a,item,beg+1,end);
}

Program to search an element in array using Binary search.

//Program to search an element in array using Binary search.
#include<stdio.h>
#include<conio.h>
void main()
{
 int *a,i,n,b,e,m,num,loc=-1;
 clrscr();
 printf("Enter the number of elements in the array u want:");
 scanf("%d",&n);
 a=(int *) malloc(n*sizeof(int));
 printf("Enter the value of elements! please make sure that list should be sorted");
 for(i=0;i<n;i++)
 {
 printf(" enter the values of %d elements: ",i);
   scanf("%d",&a[i]);
 }
 b=0;
 e=n-1;
 printf("\n enter the requried element");
 scanf("%d",&num);
 while(b<=e&&loc==0)
 {
  m=(b+e)/2;
  if(a[m]==num)
  {
    loc=m;
  }
  else
  {
   if(a[m]>num)
   {
    e=m-1;
   }
   else
   {
    b=m+1;
   }
  }
 }
 if(loc==-1)
 printf("\n element is not found");
 else
 printf("element found at location %d: ",loc);
 getch();
}

Next Home
Twitter Delicious Facebook Favorites More