Binary Search

#include<stdio.h>
int main()
{
    int a[] ={5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55};

    int n, low, heights, m;

    printf("Enter any number : ");
    scanf("%d", &n);

    low = 0;
    heights = 10;

    while(low <= heights)
    {
        m = (low + heights)/2;

        if(n == a[m])
            break;

        else if(n < a[m])
            heights = m - 1;

        else
            low = m + 1;
    }

    if(low > heights)
        printf("%d is not found\n", n);

    else
        printf("%d is found in this Array.It is found at %d position\n", a[m], m+1);

    return 0;
}

Comments