Created
October 23, 2018 18:37
-
-
Save Tester2009/d38d9f5065b08489e0f25c4b4fa06104 to your computer and use it in GitHub Desktop.
Find data in array by using binary search algorithm. Written in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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