#69 Python Tutorial for Beginners | Binary Search Using Python

#69 Python Tutorial for Beginners | Binary Search Using Python

Telusko

5 лет назад

398,715 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Ravi Soni
Ravi Soni - 16.09.2023 14:49

How is the code running with semicolons ????

Ответить
Shubham Biswas
Shubham Biswas - 15.08.2023 20:23

linear search for loop(assignment):
pos = -1
def search(list,n):
for i in range(0,len(list)):
if list[i]==n:
globals()['pos'] = i
return True
return False

list = [3,1,6,9,2]
n = 9
if search(list,n):
print('found',pos)
else:
print('not found')

Ответить
Asuradevan
Asuradevan - 03.08.2023 16:46

position (pos=0) can be declared like this also because The In search function we are modifing the pos at iteration

Ответить
MANSI MISHRA
MANSI MISHRA - 31.07.2023 18:01

I write code like what you tell sir but it's not working please find error ...

pos = -1
def search (list,n):
l=0
u= length(list)-1
while l<=u:
mid=(l+u)//2
if list [mid]==n:
globles()["pos"]=mid
return True
else:
if list [mid]<n:
l=mid+1
else:
u=mid-1
return False
list=[20,36,45,58,88,98]
n=88
if search (list,n):
print("found at", pos+1)
else:
print("not found")

Ответить
pranish N
pranish N - 28.06.2023 05:18

My question is does binary search is better than index list method...?, why we have to use, although we index method..? Can anyone please explain.

Ответить
troll o troll
troll o troll - 18.06.2023 22:41

Sir it not give last value of list is our required search. Plz answer sir

Ответить
Pardhavi Paru
Pardhavi Paru - 03.05.2023 20:56

of this code there is no output generated

Ответить
Abhinav Gupta
Abhinav Gupta - 29.04.2023 08:16

I've an issue with this technique of binary search.
If the length of a list with odd numbers and we write code according to the technique that you describe, I mean setting the upper or lower bound to mid according to the searched element is great or less so we'll reach to a condition where mid will be an odd index number which is 2nd last element and upper bound which is last element of list and when we find new mid so it will always give the same index which is 2nd last element of list and it will never be changed.
Suppose length of a list is 5 and we are trying to find the last element of list so there will be a condition where lower bound will be 3 and upper bound will be 4 and when we'll be find new mid so it will be
Mid = ( 3 + 4 ) // 2 = 3 and it will repeatedly give the same index for the mid.

Ответить
Dharshan Tabjula
Dharshan Tabjula - 02.04.2023 16:00

When iam giving last element to search its not giving the correct answer it is showing before index

Ответить
Friendly Creeper
Friendly Creeper - 01.04.2023 11:57

I have completed whole python course but still I was having problem with binary searching so here I am

Ответить
Chandra shekar A
Chandra shekar A - 29.03.2023 17:26

The upper bound should be changed to u=mid-1 in the else part of else in while loop. Then this will be correct.
Since we need to decrease the upper bound to mid -1 when n is < than mid.

Ответить
Attique Ahmed
Attique Ahmed - 21.03.2023 22:26

code crashes if you search for a number not present and mid is bigger than the search number!!!

Ответить
Ramesh Shrestha
Ramesh Shrestha - 20.03.2023 20:30

Say in hindi

Ответить
mastertwitter09
mastertwitter09 - 08.03.2023 14:52

Play in 0.75 speed - you're welcome 😁

Ответить
Tahir Soban
Tahir Soban - 07.03.2023 14:42

Excellent tutorial for learning, Thanks sir. some minor corrections are u= len(list) there is no need -1 and u = mid there is no need +1... Rest of code is superb .

Ответить
Shubham Raval
Shubham Raval - 24.01.2023 11:43

code gives error when you search for the bigger number from the last number in the list

Ответить
Sarthak Haldar
Sarthak Haldar - 05.01.2023 21:56

This code is failing when search for last element of list

Ответить
Tech With Sourabh
Tech With Sourabh - 28.12.2022 19:16

Thank you Navin sir for the explanation but I have tried to implement this code which you have shown it will not give correct results on boundary values logic of assigning mid value should be change like this
if list[mid] < n:
l = mid + 1
else:
u = mid - 1

Ответить
Harsha vardhan Reddy
Harsha vardhan Reddy - 25.12.2022 08:03

sir ,why position is -1

Ответить
Spleem
Spleem - 11.12.2022 17:54

the shifting of lower and upper bound is so easy to understand, and here I was, trying to slice the list a bunch of times...

Ответить
UpSkillLearn
UpSkillLearn - 11.12.2022 12:18

Totla Takla Chaman

Ответить
Prasanna katre
Prasanna katre - 03.12.2022 18:56

Hi Navin sir, my doubt is, the code runs fine for any index position search if we do u = len(list) OR u = len(list) - 1. Why is that?

Ответить
Marian Pascu
Marian Pascu - 19.11.2022 16:03

Wait, does the return statement end the while loop? Because the condition raised at while always has to be true :/

Ответить
Vignesh M
Vignesh M - 17.11.2022 07:52

so finally list must be in ascending order.

Ответить
Vignesh M
Vignesh M - 17.11.2022 07:48

if we pass a sorted list to the function like(if sorted(List):) why it is not searching for the element?

Ответить
Terminal Magic
Terminal Magic - 10.11.2022 07:13

Problem is , you are finding pivot only once , but pivot element will be searched everytime you did not find the required key.

Ответить
YARRAMNEEDI RAVINDRASWAMY
YARRAMNEEDI RAVINDRASWAMY - 02.11.2022 13:55

the below code not giving output

Ответить
YARRAMNEEDI RAVINDRASWAMY
YARRAMNEEDI RAVINDRASWAMY - 02.11.2022 13:55

pos = -1

def search(list_a,n):
l = 0
u = len(list_a)-1

while l <= u:
mid = (l+u) // 2
if list_a[mid] == n:
global pos
pos = mid
return True
else:
if list_a[mid] < n:
l = mid
else:
u = mid
return False



list_a = [1,2,3,45,98,10987]
n = 10987

if search(list_a,n):
print('the element found',pos)
else:
print('not found')

Ответить
My Monologues and Acting Here
My Monologues and Acting Here - 13.10.2022 11:45

Baap hai bhai tuklu boss programming smajhane mein

I wish my clg teacher were like that but woah toh tatti hai

Ответить
Suchitra Chingkhamayum
Suchitra Chingkhamayum - 10.10.2022 19:44

Sir could you make one about postorder traversal

Ответить
Vishak Arudhra
Vishak Arudhra - 24.06.2022 16:04

Naveen, I haven't found even native english speakers to explain as intuively as you do in this video. Thanks a lot for what you do.

Ответить
AKRAM
AKRAM - 26.04.2022 11:45

Sir aap doubts ke liye Jhonny sins ko sb doubts ki le lete ho

Ответить
Yashwantha A S
Yashwantha A S - 28.03.2022 13:40

Your videos are far better than others

Ответить
sahab jaday
sahab jaday - 14.03.2022 12:35

there is one error occuring using this code if i am taking the len of list as odd ,some bugs are showing .if anyone knows plz do help

Ответить
AD PRINCE
AD PRINCE - 04.03.2022 22:48

the program otp is not working crtly

pos = -1

def search(list, n):


l = 0
u = len(list)-1

while l <= u:
mid = (l+u)//2

if mid == n:
globals()["pos"]=mid
return True
else:
if list[mid]<n:
l = mid+1
else:
u = mid-1
return False


list = [1,2,3,4,5,6,7,8,9,111,229,353,474,545,676,977,87778,999999]
n =545

if search(list,n):
print('found at',pos+1)
else:
print("not found")



otp='not found'
can anyone help me with this !?

Ответить
Ramunaidu Borusu
Ramunaidu Borusu - 28.02.2022 08:36

code not working ....

Ответить
rama Gopal
rama Gopal - 22.02.2022 17:32

Sir we put n value as a User input…,'…

Ответить
kitsune2101
kitsune2101 - 10.02.2022 09:06

This is freaking me out, like why this is working, this shouldn't work!!!!!!!!
pos=0
def search(list, n):
l=0
u=len(list)


for i in range(l,u):

mv=(l+u)//2

if list[mv] == n:
globals()['pos'] = mv
return True
else:
if list[mv] < n:
l=mv
else:
u=mv



list1=[3,5,7,98,100,345,6789,34556]
n=34556

if search(list1, n):
print("found",pos)
else:
print("not found")

Ответить
uday kumar Narayana
uday kumar Narayana - 19.12.2021 19:28

lst=[5,8,4,9,6,2]
lst.sort()
print(lst)
searchValue=2
low = 0
up=(len(lst) - 1)
for i in range(len(lst)):
index = (low + up // 2)
if lst[low] <searchValue and searchValue <lst[index]:
print("lower", low, up)
up= index
low=low
elif lst[low] < searchValue and searchValue < lst[index]:
print("upper", low, up)
low = index
up = up
elif lst[low]==searchValue:
print("equal at position ",low)
break
elif lst[up]==searchValue:
print("equal at position ", up)
break
else:
print("element not found.")
break

Ответить
ayush gupta
ayush gupta - 17.12.2021 17:39

why we did +1 and -1 in the conditional statements??

Ответить
Akmal Kk
Akmal Kk - 04.11.2021 14:27

why we didnt use i+=1 inside while loop ?

Ответить
piyush patil
piyush patil - 28.10.2021 11:24

def Binary(lst,n):

l = 0
h = len(lst) -1
for i in range((len(lst))//2):
m = (l+h) // 2
if lst[m] == n:
print("Found at", m)
break
elif lst[m] > n:
h = m
elif lst[m] < n:
l = m
else:
print("Not Found")
lst = [4, 7, 8, 12, 45, 99]

Binary(lst,45)

Ответить
Roszelli Clothings
Roszelli Clothings - 23.10.2021 14:10

I really appreciate this video. Thanks a lot

Ответить
VladGalagan
VladGalagan - 13.10.2021 17:49

What would one's do if you should to find couple numbers in list?

Ответить
Sombith Sen
Sombith Sen - 09.10.2021 08:53

Sir this code isn't working for me.

Ответить
Farhan Khan
Farhan Khan - 06.09.2021 14:54

Did he just called me an alien?

Ответить
Muhammad Abul Hassan
Muhammad Abul Hassan - 04.09.2021 20:12

position=-1
def search(list,m):
lower_bound=0
upper_bound= len(list)-1
while lower_bound <= upper_bound:
mid=(lower_bound+upper_bound)//2
if list[mid]==m:
return True
else:
if list[mid]>m:
lower_bound=mid+1
else:
upper_bound=mid-1
return False


list=[]
n=int(input("Enter Total Elements: "))
for i in range(n):
elements_of_list=int(input("Enter Elements of list: "))
list.append(elements_of_list)
print(list)
m=int(input("Enter Value to search"))
if search(list,m):
print("Found", position+1)
else:
print("Not Found")

Ответить
☆ Wolfii ☆
☆ Wolfii ☆ - 28.08.2021 17:39

Great explation! But at the part when you run it and says at pos 5 dont you start counting from 0 when indexing? Because if you start at 0 and count to 5 you get 99 not 45.

Ответить