Tuesday, March 22, 2011

program for Binary search in data structure with example


#include <stdio.h>
#define MAX 10

void bsearch(int list[],int n,int element)
{
   int l,u,m, flag = 0;
   l = 0;
   u = n-1;
   while(l <= u)
   {
      m = (l+u)/2;
      if( list[m] == element)
      {
               printf(" The element whose value is %d is present at
position %d in list\n",
                     element,m);
                 flag =1;
                 break;
      }
      else
            if(list[m] < element)
                   l = m+1;
            else
                   u = m-1;
   }
   if( flag == 0)
   printf("The element whose value is %d is not present in the list\n",
       element);
}

void readlist(int list[],int n)
{
   int i;
   printf("Enter the elements\n");
   for(i=0;i<n;i++)
       scanf("%d",&list[i]);
}

void printlist(int list[],int n)
{
    int i;
   printf("The elements of the list are: \n");
   for(i=0;i<n;i++)
       printf("%d\t",list[i]);
}

void main()
{
   int list[MAX], n, element;
   printf("Enter the number of elements in the list max = 10\n");
   scanf("%d",&n);
   readlist(list,n);
   printf("\nThe list before sorting is:\n");
   printlist(list,n);
   printf("\nEnter the element to be searched\n");
   scanf("%d",&element);
   bsearch(list,n,element);
}

Example

Input
Enter the number of elements in the list, max = 10
10
Enter the elements
34
2
1
789
99
45
66
33
22
11
Output
The elements of the list before sorting are:
34  2  1  789  99  45  66  33  22  11
1   2  3   4    5   6   7   8   9  10
Enter the element to be searched
99
The element whose value is 99 is present at position 5 in the list

No comments:

Post a Comment