Skip to content

Instantly share code, notes, and snippets.

@Tester2009
Created October 23, 2018 18:37
Show Gist options
  • Save Tester2009/d38d9f5065b08489e0f25c4b4fa06104 to your computer and use it in GitHub Desktop.
Save Tester2009/d38d9f5065b08489e0f25c4b4fa06104 to your computer and use it in GitHub Desktop.
Find data in array by using binary search algorithm. Written in Python
# explanation: https://www.youtube.com/watch?v=IcK2Qyk3cUs
# written by: github.com/Tester2009
def binary_search(arr, val):
if len(arr)==0 or (len(arr)==1 and arr[0]!=val):
return False
mid = arr[len(arr)/2]
if val==mid:
print("Found")
if val<mid:
return binary_search(arr[:len(arr)/2], val)
if val>mid:
return binary_search(arr[len(arr)/2+1:], val)
array_1 = [1, 2, 3, 5, 7, 9]
binary_search(array_1, 7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment