Binary Search C Program

Thursday, 5 September 2013

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a[10],lb,ub,mid,i,key,n;
    clrscr();
    printf("Enter the no. of element in the array \n");
    scanf("%d",&n);
    printf("enter the array elements \n");
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    printf("enter the key element to search \n");
    scanf("%d",&key);
    lb=0;
    ub=n-1;
    mid=(lb+ub)/2;
    while(lb<=ub&a[mid]!=key)
    {
    if(key<a[mid])
    ub=mid-1;
    else
    lb=mid+1;
    mid=(lb+ub)/2;
    }
    if(a[mid]==key)
    {
    printf("element is found");
    }
    else
    {
    printf("element is not found");
    }
    getch();
    }

OUTPUT:-

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
Related Posts Plugin for WordPress, Blogger...
 

Most Reading

Labels