-
Notifications
You must be signed in to change notification settings - Fork 17
/
binarysearch.py
43 lines (36 loc) · 1.17 KB
/
binarysearch.py
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
42
43
'''
Binary Search
Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half.
'''
# iterative approach
def binarysearch(list, target):
left = 0
right = len(list)-1
while left <= right:
mid = (left+right)//2
if target == list[mid]:
return mid
elif target > list[mid]:
left = mid+1
else:
right = mid-1
return -1
print(binarysearch([2, 3, 4, 5], 5)) # 3
# recursive approach
def binarysearch(list, left, right, target):
if right >= left:
mid = (left+right)//2
if target == list[mid]:
return mid
# If element is smaller than mid, then it can only
# be present in left subarray
elif list[mid] > target:
return binarysearch(list, left, mid-1, target)
# Else the element can only be present in right subarray
else:
return binarysearch(list, mid+1, right, target)
else:
return -1
list = [2, 3, 4, 10, 11, 16, 90, 32, 7]
target = 16
print(binarysearch(list, 0, len(list)-1, target)) # 5