Saturday, 15 September 2018

Program to do binary search of element in C

What is searching ?

It is defined as find data of a element. We use different types of searching techniques for searching an element.
               Here we use binary search techniques in C.

Program to do binary search of element in C 

#include<conio.h>
void main ()
{
int data[10],beg,mid,end,item,lb,ub,i;
clrscr ();
printf ("\n Enter 5 element in selected order:");
scanf ("%d", &item);
beg=0;
end=4;
mid=(beg+end)/2;
while(beg<=end && data[mid]!=item)
{
      if (item>data[mid])
     {
      beg=mid+1;
      }
      else
       {
      end=mid-1;
       }
      mid=(beg+end)/2;
}
if(beg<end)
{
   printf("\n location = %d",mid+1);
}
else
{
   printf("\n unsuccessful:");
}
getch ();
}

OUTPUT ::  Enter 5 element in selected order = 25,45,65,75,85

                 ITEM SEARCHED ; 45

1 comment:

Featured post

Program to find Volume of given box in java

class volume { public static void main (String args []) { int l=5,b=6,h=10; int volume; volume=l*b*h; System.out.println (" vol...