0
2.2kviews
Write a program to implement Binary search on sorted set of integers
1 Answer
0
101views
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
    int n,i,search,f=0,low,high,mid,a[100];
    clrscr();
    printf("Enter the size of array");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("Enter a[%d] ",i);
        scanf("%d",&a[i]);
    }
    printf("Enter the search element:");
    scanf("%d",&search);
    low=0;
    high=n-1;
    while(low<=high)
    {
        mid=(low+high)/2;
        if(search<a[mid])
        {
        high=mid-1;
        }
        else if(search>a[mid])
        {
        low=mid+1;
        }
        else
        {
        f=1;
        printf("Element found at %d:",mid);
        exit(1);
        }
    }
    if(f==0)
    {
        printf("not present");
    }
    getch();
 }
Please log in to add an answer.