Computer Science/Data Structure
Binary Search (이분탐색)
재키재키
2022. 5. 4. 18:55
1. 알고리즘
- 탐색시간은 O(logN)이다.
- 정렬 알고리즘은 다음과 같다.
left = 0
rigth = n - 1
while left < right:
mid = (left + right) / 2
if arr[mid] < target:
left = mid + 1
else:
right = mid
if (left == right) and (arr[left] == x):
found = True
foundpos = left
else:
found = False