Skip to content
Advertisement

Binary Search – Is this correct? and how to get position of search term

I am a beginner to programming. I have three questions about my binary search code I wrote (in Python): (1) When I compared my code to the ones I found on the net, I see that everyone uses low and high values parameters. Wondering if mine can have errors that I am not seeing or is it just that the high/low parameters make the code more readable? I thought mine follows the recursive way of thinking (at least for me).

(2) edit: This part has since been answered.—>> I can’t get it to return True or False for the answer. Is it because the True/False is getting returned to the recursive function and not the print? How can I get it to return True, or False answer?

(3) I can’t figure out how to get the position of the search term in the list (of course, not using index function). I thought I could somehow use the “mid” variable which was in the code, but couldn’t. Can you give me some ideas how to get its position in the list?.

JavaScript

Thanks

Arun

Advertisement

Answer

The problem here is that you’re not returning the value on the recursive cases. Refactor your code to this and it should work.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement