Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b3tGdV3Vm51QUZxg2x
21 lines
737 B
Plaintext
21 lines
737 B
Plaintext
Iterative binary search, traced step by step on [10, 20, 30, 40, 45]
|
|
====================================================================
|
|
|
|
search for 40
|
|
low=0 high=4 mid=2 a[mid]=30 -> 40 > 30, low=3
|
|
low=3 high=4 mid=3 a[mid]=40 -> equals 40, return 3
|
|
result: 3 (position 4 counting from 1), 2 elements compared
|
|
|
|
search for 10
|
|
low=0 high=4 mid=2 a[mid]=30 -> 10 < 30, high=1
|
|
low=0 high=1 mid=0 a[mid]=10 -> equals 10, return 0
|
|
result: 0 (position 1 counting from 1), 2 elements compared
|
|
|
|
search for 25
|
|
low=0 high=4 mid=2 a[mid]=30 -> 25 < 30, high=1
|
|
low=0 high=1 mid=0 a[mid]=10 -> 25 > 10, low=1
|
|
low=1 high=1 mid=1 a[mid]=20 -> 25 > 20, low=2
|
|
low=2 high=1 -> low > high, return -1
|
|
result: -1, 3 elements compared
|
|
|