0
Anyone knows how to do binary search 🤨 using for loop in python
plzz answer*******😟🎷
16 Answers
+ 2
I think this works. I only changed one line in a typical binary search algorithm. As mentioned before, I just calculated the number of iterations in the worst case, and used a for loop with that range.
Let me know if you find any bugs. Cheers!
https://code.sololearn.com/cgy1UH9euwnE/?ref=app
+ 2
NASH if you don't mind, could you please share the code? Thanks!
+ 1
Binary search tutorial with explanation!!
https://www.techinfected.net/2016/11/JUMP_LINK__&&__python__&&__JUMP_LINK-program-for-binary-search.html?m=1
+ 1
AL Araf the code in your link uses while loop for the binary search.
NASH, it's possible to calculate how many iterations of the loop the binary search would need in the worst case, and then use a for loop with that range. But why do you want to use a for loop when while loop or recursion are simpler choices here?
+ 1
Great! Then you can try what I suggested. If I do it for you, you won't learn as much 😊
+ 1
The main function is in lines 8-19. The rest is for testing that it works.
If this looks too long, why don't you share your favorite binary search code that uses a while loop, and I'll turn it into a for loop version having the same number of lines? Like I said before, I only modified one line in a typical binary search code.
0
Ahh experiment with all format guys when you learn any lang
0
😕😕 so Saha I quit!!!😵 if you know this solution you tell me if not this becomes all okk👻
0
thxx but this big chunk of code I couldn't handle☺️☺️
0
do it shory
0
do it short
0
yaa of course
0
burrah i do it
0
finally i got this right
0
after long time
0
okk friend