Binary Search Iterative method in python

This is an iterative method for the binary search algorithm. It takes a list as an input and another variable which is the data to be searched, and then passes them to the bin() function. And in bin() there is a single while loop.

Screenshots/ Output :


The Code
def bin(x,y):
    l=0;
    h=len(x)-1;
    while(l<=h):
        mid=(l+h)/2
        if(x[mid]<y):
            l=mid+1
        elif(x[mid]>y):
            h=mid-1
        elif(x[mid]==y):
            print "found "
            break;
        else:
            print "Not Found "
x=input()
y=input()
bin(x,y);

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Bit Stuffing Code Implementation in Java

Employee Management System Using Inheritance in Java