Wednesday, 7 November 2012

WAP to perform BINARY search.

  • WAP to perform BINARY search.(Bubble Sorting needed) .
#include <conio.h>
void main()
{
    int a[10],i,j,num,lb=0,ub=9,mid;
    printf("Enter the array element");
    for(i=0;i<10;i++)
    scanf("%d",&a[i]);
    printf("Enter the search element");
    scanf("%d",&num);
//Bubble Sort
    for(i=0;i<10;i++)
    {
        for(j=i+1;j<10;j++)
        {
            //Ascending Order
            if(a[i]>a[j])
            a[i]=(a[i]+a[j])-(a[j]=a[i]);
        }
    }
//Searching Process
    while(lb<=ub)
    {
        mid=(lb+ub)/2;
        if(num==a[mid])
        break;
        if(num>a[mid])
        lb=mid+1;
        else
        ub=mid-1;
    }
    if(lb>ub)
    printf("Not Found");
    else
    printf("Found");
    }

No comments:

Post a Comment