-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBinary Search.py
More file actions
49 lines (39 loc) · 1.28 KB
/
Binary Search.py
File metadata and controls
49 lines (39 loc) · 1.28 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
42
43
44
45
46
47
48
49
"""
Binary Search
Write a function that takes in a sorted array of the integers as well as a target.
The function should use the Binary search algorithm to determine if the target integer
is contained in the array and should return its index if it is ,otherwise -1.
Sample Input
array = [0, 1, 21, 33, 45, 61, 71, 72, 72, 73]
target = 33
Sample Output
3
"""
# SOLUTION 1
# Running time O(logn) | space O(1)
def binarySearch1(array, target):
left = 0
right = len(array) - 1
while left <= right: # or not (left > right)
mid = (left + right) // 2
if target > array[mid]:
left = mid + 1
elif target < array[mid]:
right = mid - 1
else:
return mid
return -1
# SOLUTION 2
# time O(logn) | space O(logn)
def binarySearch(array, target):
return binarySearchHelper(array, target, 0, len(array) - 1)
def binarySearchHelper(array, target, left, right):
if left > right:
return -1
mid = (left + right) // 2
if target > array[mid]:
return binarySearchHelper(array, target, mid + 1, right)
elif target < array[mid]:
return binarySearchHelper(array, target, left, mid - 1)
else:
return mid