Tuesday, 8 March 2016

C Program For Binary Search


#include
int main()
{
//Ghanendra Yadav
    int a[10],i,n,m,c=0,l,u,mid;

    printf("Enter the size of an array: ");
    scanf("%d",&n);

    printf("Enter the elements in ascending order: ");
    for(i=0;i {
         scanf("%d",&a[i]);
    }

    printf("Enter the number to be search: ");
    scanf("%d",&m);

    l=0,u=n-1;
    while(l<=u)
{
         mid=(l+u)/2;
      #include

int main()
{
//Ghanendra Yadav
    int a[10],i,n,m,c=0,l,u,mid;

    printf("Enter the size of an array: ");
    scanf("%d",&n);

    printf("Enter the elements in ascending order: ");
    for(i=0;i {
         scanf("%d",&a[i]);
    }

    printf("Enter the number to be search: ");
    scanf("%d",&m);

    l=0,u=n-1;
    while(l<=u)
{
         mid=(l+u)/2;
         if(m==a[mid])
{
             c=1;
             break;
         }
         else if(m {
             u=mid-1;
         }
         else
             l=mid+1;
    }
    if(c==0)
         printf("The number is not found.");
    else
         printf("The number is found.");

    return 0;
}

Output:-



No comments:
Write comments

Recommended Posts × +