- 1

How to do binary search in python?

I want a full complete program to understand my bug. I have tried multiple times but it has not worked. So can you show a full program for it?

17th Jun 2020, 2:38 PM
Asim Yadav
Asim Yadav - avatar
7 Answers
+ 3
l = 0, r = last index while l <= r: mid = l + (r - l) / 2 if v[mid] < target l = mid + 1 else if v[mid] > target r = mid - 1 else return mid return -1
17th Jun 2020, 3:12 PM
Bobby Fischer
Bobby Fischer - avatar
+ 3
Before asking for a "complete full program", you should do a try by yourself. Please put it in the playground and link it here. Thanks!
17th Jun 2020, 3:37 PM
Lothar
Lothar - avatar
+ 2
Hmm no there are several tutorials ,probably code also our there ,why don't you first understand how binary search works and then try to implement ,if still you are facing an errors link the code so someone can help you ,also do not ask for codes here :-)
17th Jun 2020, 2:50 PM
Abhay
Abhay - avatar
+ 2
Asim Yadav, yes you did, but you have not presented any of your codes here. If we can see what you have done so far, it is much easier to to support you. How should we know about the bug you mentioned?
17th Jun 2020, 4:48 PM
Lothar
Lothar - avatar
0
Lothar didn't I say in my description that "I have tried multiple times".
17th Jun 2020, 3:42 PM
Asim Yadav
Asim Yadav - avatar
17th Jun 2020, 3:43 PM
Asim Yadav
Asim Yadav - avatar
0
Lothar ok I will attach the image of my code
17th Jun 2020, 4:49 PM
Asim Yadav
Asim Yadav - avatar