forked from souravjain540/Basic-Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.py
More file actions
41 lines (30 loc) · 1.26 KB
/
binary_search.py
File metadata and controls
41 lines (30 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Python script to find a given number in a sorted list using binary search
# The algoritem splits the list in half and checks if the value in the middle
# equals the given number, if it isnt, continuing with the first half of the list
# otherwise continuing with the second half
# Public function which calls a private function with extra parameters.
def find_number(num,lst):
return __find_number(num,lst,len(lst)-1,0)
# Private function
def __find_number(num,lst,high,low):
# Base case
if high >= low:
# Setting mid as the middle index of the list
mid = (high+low)//2
# The element in index mid equals our number? YAY we found it!
if lst[mid] == num:
return mid
# The element is greater than our number - we passed it, it should be on the other half of the list.
elif lst[mid] > num:
# Indexing the list to start from to the mid
return __find_number(num,lst,mid-1,low)
else:
# Indexing the list from the mid to high
return __find_number(num,lst,high,mid+1)
# Couldnt find the number!
else:
return -1
# Main
if __name__ == "__main__":
lst= [1,2,3,5,6,7,10,15]
print(find_number(10,lst))