categories: computer science
Hello everyone,
Below you will find a video with a detailed explanation of the binary search algorithm. Its operation, time complexity, and usefulness. Below you will also find ready-made code to copy if needed.
Code:
a = [1, 2, 3, 4, 5]
#------------------------------
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#------------------------------
def binarySearch(x, a):
if x < a[0]:
return False
elif x > a[len(a) - 1]:
return False
poczatek = 0
koniec = len(a)
while poczatek != koniec:
srodek = (poczatek + koniec) // 2
if x < a[srodek]:
koniec = srodek - 1
elif x > a[srodek]:
poczatek = srodek + 1
if x == a[srodek]:
return True
return False
if binarySearch(b[4], a):
print("tak")
else:
print("nie")
Video:
Thank you for reading!
Read more