Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Implement Binary Search Using C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 393
    Comment on it

    Binary Search:- The Binary Search is one of the most important searching technique in Searching Algorithm. The one of property of this searching algorithm is, all the elements are in sorted order in array. The worst case and average case complexity of this algo is O(log n) and gives O(1) in best case.


    This algorithm is based on divide and conquer approach.firstly we define the lower and upper bound of array This algo first find the middle element in the array. if searching element is less than the mid(middle) element it set mid+1 element to up(upper bound) and if searching element is greater than the mid(middle) element it set mid-1 element to lw(lower bound). If the mid(middle) element is equal to searching element the element is found.

    Worst Case:- This indicate that the searching element is found at last position or first position in the array.

    Best Case:- This indicate that the searching element is found at the middle in the array at first division(lw+up/2).

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int i=0,se=0,n,s,e[1000],lw=0,mid=0,up=0;
    clrscr();
    printf("Enter the size of an Array:=");
    scanf("%d",&s);
    
    printf("Enter the Element of an Array\n");
    for(i=0;i<s;i++)
    {
    scanf("\n%d",&e[i]);
    }
    printf("Enter the Number You Want to Search:=");
    scanf("%d",&se);
    up=s-1;
    
    while(lw<=up)
    {
    mid=(lw+up)/2;
    printf("%d",mid);
    
        if(se>e[mid])
        {
            lw=mid+1;
        }
        if(se<e[mid])
        {
            up=mid-1;
        }
        if(se==e[mid])
        {
            printf("%d is found at position %d",se,mid+1);
            break;
        }
        if(lw>up)
        {
            printf("%d is not found",se);
        }
    }
    getch();
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: