Implement Binary search in C

Aim: Implement binary search in C.

#include <stdio.h> 
int main(void) 
{ 
    int arr[20],i,n,item,l=0,result=-1,r;

    printf("Enter the number of elements: ");
    scanf("%d",&n);

    printf("Enter elements in sorted order: ");
    for(i=0;i<n;i++)
        scanf("%d",&arr[i]);
        
    printf("Enter the item to be searched: ");
    scanf("%d",&item);
    r=n-1;
    while (l <= r) { 
        int m = l + (r - l) / 2; 
   
        if (arr[m] == item) 
            result=m; 
   
        if (arr[m] < item) 
            l = m + 1; 
        else
            r = m - 1; 
    } 
    if(result == -1)
        printf("Element not found in array");
    else
        printf("Element found at position %d", result+1); 
    return 0; 
} 
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments