Slide 23
Slide 23 text
線形探索と二分探索を例に
23
def 線形探索(arr: list[int], target: int) -> int:
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
def 二分探索(arr: list[int], target: int) -> int:
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
if arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
🎨 Colors: Catppuccin Latte from https://github.com/catppuccin/catppuccin