Python program to Make a Binary Search of Number in an Array

from random import random
N = 20
array = []
for x in range(N):
 array.append(int(random()*100))
array.sort()
print(array)
number = int(input("Search for any number in the array: "))
mini = 0
maxi = N-1
while mini <= maxi:
 mid = (mini + maxi) // 2
 if number < array[mid]:
  maxi = mid-1
 elif number > array[mid]:
  mini = mid+1
 else:
  print("The number is located at index: ", mid)
 break
else:
 print("There is no number!")

 

 

Output:

[9, 12, 13, 33, 34, 45, 45, 46, 53, 59, 61, 67, 78, 79, 80, 81, 82, 91, 93, 94]
Search for any number in the array: 59
The number is located at index:  9

Subscribe For Daily Updates