-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search.py
37 lines (35 loc) · 842 Bytes
/
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
def linear(arr, key):
"""
:type arr: array, np.array
:type key: int,byte, float, ...
:return: boolean
"""
flag = False
for i in range(len(arr)):
if arr[i] == key:
flag = True
break
return flag
def Binary(arr, key):
"""
:type arr: list or numpy.array
:type key: int, float, double, ...
:return: boolean
"""
left = 0
right = len(arr) - 1
flag = False
while left <= right:
mid = int((left + right) / 2)
if arr[mid] == key:
print('Found at %d' % mid)
flag = True
break
else:
if key > arr[mid]:
left = mid + 1
else:
right = mid - 1
if not flag:
print('not Found')
return flag