Created
May 5, 2019 14:49
-
-
Save sumukus/4a2d088c94bc5bcaf8e7f64cbdcadf63 to your computer and use it in GitHub Desktop.
Search Algorithms
This file contains hidden or 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
def binarySearch(data,search): | |
while len(data) > 1: | |
mid=len(data)//2 | |
if data[mid] is search: | |
return True | |
elif data[mid] > search: | |
return binarySearch(data[:mid],search) | |
else: | |
return binarySearch(data[mid:],search) | |
if len(data) is 1: | |
if data[0] is search: | |
return True | |
#data contains the sorted list of elements in ascending order | |
#serach is the element to be searched in data. | |
data=[1,2,4,5,7,21,31] | |
search=21 | |
status=binarySearch(data,search) | |
if status is True: | |
print("Search element {} is Found".format(search)) | |
else: | |
print("Search element {} is not Found".format(search)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment