Binary search

#include <stdio.h>
int main()
{
   int c, a, b, y, n, search, array[100];

   printf("Enter number of elements to be printed  :\t");
   scanf("%d",&n);

   printf("Enter %d integers : \t", n);

   for (c = 0; c < n; c++)
      scanf("%d",&array[c]);

   printf("Enter value to find  : \t ");
   scanf("%d", &search);

   a = 0;
   b = n - 1;
   y = (a+b)/2;

   while (a <= b) {
      if (array[y] < search)
         a = y + 1;
      else if (array[y] == search) {
         printf("%d found at location %d.\n", search, y+1);
         break;
      }
      else
         b = y - 1;

      y = (a + b)/2;
   }
   if (a > b)
      printf("Not found! %d is not present in the above list.\n", search);

   return 0;
}




OUTPUT : -




For Other Programs Visit The WebSite:-   https://www.techapurba.com/

Follow Me On Social Media :-

For tech related videos visit my other website :- https://apurbatechinfo.blogspot.com/

Post a Comment

0 Comments