-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_search.py
43 lines (24 loc) · 1.09 KB
/
binary_search.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
def BibarySerch(Arryas, target):
left = 0
right = len(Arryas)-1
loop = 1
while left <= right :
middle = (left + right) //2
mid_ele = Arryas[middle]
print(f'{mid_ele} at loop number {loop}')
loop += 1
if mid_ele == target:
return middle
elif mid_ele > target:
right = middle - 1
else :
left = middle + 1
return "element not found"
print(BibarySerch([2,4,6,8,12,14], 8))
"""
The time complexity of the binary search algorithm implemented in the provided code is O(log N), where N is the number of elements in the array. This is because the search space is halved in each iteration of the while loop, leading to a logarithmic time complexity.
The space complexity of the code is O(1) because it uses a constant amount of additional memory. The variables left, right, middle, and mid_ele require a constant amount of space, regardless of the size of the input array.
In summary:
Time complexity: O(log N)
Space complexity: O(1)
"""