CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List
Python Code Walkthrough #50

Each Mid Of A Binary Search

hardData Structures & Algorithms

📄 Code

Read carefully — what does this print?
1def search(items: list[int], target: int) -> int:
2 low: int = 0
3 high: int = len(items) - 1
4 while low <= high:
5 mid: int = (low + high) // 2
6 print("mid", mid, items[mid])
7 if items[mid] == target:
8 return mid
9 if items[mid] < target:
10 low = mid + 1
11 else:
12 high = mid - 1
13 return -1
14print(search([2, 5, 8, 12, 16, 23, 38], 7))

🎯 Your Answer