CJCoding With Joseph
15free / daySign inCreate account

Binary Search Iterative

Write binary_search(values: list[int], target: int) -> int, the iterative version: the list is already sorted, so keep low and high indexes for the part that may still hold the target, and while low is at most high compute mid as (low + high) // 2; return mid when values[mid] equals the target, move low up past mid when the middle value is too small, otherwise move high down below mid. Return -1 when the range empties. Each step halves the range, so this is O(log n). Do NOT write a main block or any top-level test code; the tests construct the calls. Input: two lines, a space-separated sorted list of integers (possibly empty) and then the target; the tests print the index your function returns.

Expected Output:

3

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Data Structures & AlgorithmsBig-O
📥 Auto-Input:
1 3 5 7 9 11\n7\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.