This is a program to perform Binary Search on a sorted array


In [41]:
A = []
A = input("Input a sorted array ")
n = input("Enter number to be searched ")


Input array 1 2 5 6
Enter number to be searched 3

In [42]:
m1 = 0
m2 = len(A) - 1
while m1 <= m2:
    mid = int((m1 + m2) / 2)
    if A[mid] == n:
        print(n, "found")
        break;
    elif A[mid] > n:
        m2 = mid - 1
    else:
        m1 = mid + 1
if m2 < m1:
    print(n, "not found")


3 not found