這篇筆記是我參考【演算法圖鑑】和網路資源所製作。如果內容有誤,還請不吝賜教!
Search in an Array
Linear Search
- Operations
- Traverse through the array until our goal is found.
- Time Complexity -
- Implementation
def find(arr: list or tuple, n) -> int:
for i in range(len(arr)):
if arr[i] == n:
return i
else:
return -1
l = [2, 5, 9, 4, 2, 3, 7, 6]
print(find(l, 3)) # 5
print(find(l, 1)) # -1
Binary Search
This algorithm only works for sorted array.
- Operations
- Let be the middle data of the array .
- If equals to our goal , our searching is finished.
- Else if , which indicates could appears in the subarray of to the right of (), our searching range is now confined to the left subarray.
- Otherwise, i.e. may appears in the subarray of to the right of (), our searching range is confined to the right subarray.
- Repeat 1-4 until we find our goal, however if becomes an empty array, we return " is not in the array".
- Time Complexity -
- Implementation
# !! only for sorted array
def binarySearch(arr: list or tuple, n) -> int:
l, r = 0, len(arr)
while l <= r:
idx = (l + r) // 2
if arr[idx] == n:
return idx
elif arr[idx] > n:
r = idx - 1
else:
l = idx + 1
else:
return -1
l = sorted(rint(1, 10) for i in range(10))
print(l)
print(binarySearch(l, 6))
wa = 0
for i in range(1, 11):
l = [j for j in range(1, 11)]
if binarySearch(l, i) != find(l, i):
wa += 1
print('###', binarySearch(l, i), end=' ')
print(i)
print(wa) # I'm very sure it's 0.