Posts

Showing posts with the label Write a Program to perform in c Binary search for a given value in a sorted list

Write a Program to perform in c Binary search for a given value in a sorted list

/* Write a Program to perform in c Binary search for a given value in a sorted list*/ #include <stdio.h> int main() {   int i, first, last, middle, n, item, la[100];   printf("Enter number of elements\n");   scanf("%d", &n);   printf("Enter %d integers\n", n);   for (i = 1; i <= n; i++)     scanf("%d", &la[i]);   printf("Enter value to find\n");   scanf("%d", &item);   first = 1;   last = n;   middle = (first+last)/2;   while (first <= last) {     if(la[middle] == item)     {         printf("found ");         break;     }       else if (item>la[middle])     {       first = middle + 1;     }     else     {       last = middle - 1;     }     middle = (first + last)/2;   }   if (first > last)     printf("Not found");   return 0; }