Searching 5


Given a sorted array arr[] of n elements, write a program using binary search to search a given element x in arr[]. \n\nInput:\nNumber of elements, elements in sorted order and finally the element to be searched in the array.\n\nOutput:\nThe location where the element is found.


#include <stdio.h>
int main()
   int c, first, last, middle, n, search, array[100];
   for (c = 0; c < n; c++)
   scanf("%d", &search);
   first = 0;
   last = n - 1;
   middle = (first+last)/2;
   while (first <= last) {
      if (array[middle] < search)
         first = middle + 1;    
      else if (array[middle] == search) {
         printf("%d found at location %d\n", search, middle+1);
         last = middle - 1;
      middle = (first + last)/2;
   if (first > last)
      printf("Not found! %d is not present in the list\n", search);
	return 0;
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO