15,745,574 members
1.00/5 (1 vote)
See more:
Python
```def bsearch2 ( lst : list , key , lo :int , hi : int ):
if lo == hi :
return None # key not in empty segment
m = ( lo + hi )//2 # position of root
if lst [ m] == key :
return m
elif lst [ m] > key :
return bsearch2 ( lst , key , lo , m)
else : # lst[m] < key
return bsearch2 ( lst , key , m +1 , hi )```

What I have tried:

.................................................................
Posted
Updated 16-Sep-20 9:52am

## Solution 1

Why do you think? What would change if you didn't?

Do two things:
1) Read this: Binary search algorithm - Wikipedia[^]
And
2) Break out the debugger and follow your code through as is. Now change "m + 1" to "m" and try it again. What changed? Why didn't it work?

Try it - invest some time in looking at what happens, and think about it. That will give you a much better grasp of how algorithms work in general that just being told "because ..."

the problem i have programming exam after few days and there is no time to learn new things or techniques :D

## Solution 2

The same answer as you were given four days ago at https://www.codeproject.com/Questions/5279256/Why-do-we-add-1-to-the-r-plus-m-in-the-last-line[^]. Did you follow the suggestions?

## Solution 3

Same question as Why do we add 1 to the r + m in the last line ?[^] with mostly same code.
m+1 is not alone it foes with other variables:
Python
```elif lst [ m] > key :
return bsearch2 ( lst , key , lo , m )
else : # lst[m] < key
return bsearch2 ( lst , key , m +1 , hi )
```

Think about the meaning if those variables in each call to bsearch2.
Take a sheet of paper and a pencil, write a sorted list and simulate the meaning of each variables in the list as search is going on.

v2