Комментарии:
How is the code running with semicolons ????
Ответить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')
position (pos=0) can be declared like this also because The In search function we are modifing the pos at iteration
Ответить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")
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.
ОтветитьSir it not give last value of list is our required search. Plz answer sir
Ответитьof this code there is no output generated
Ответить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.
When iam giving last element to search its not giving the correct answer it is showing before index
ОтветитьI have completed whole python course but still I was having problem with binary searching so here I am
Ответить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.
code crashes if you search for a number not present and mid is bigger than the search number!!!
ОтветитьSay in hindi
ОтветитьPlay in 0.75 speed - you're welcome 😁
Ответить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 .
Ответитьcode gives error when you search for the bigger number from the last number in the list
ОтветитьThis code is failing when search for last element of list
Ответить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
sir ,why position is -1
Ответить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...
ОтветитьTotla Takla Chaman
Ответить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?
ОтветитьWait, does the return statement end the while loop? Because the condition raised at while always has to be true :/
Ответитьso finally list must be in ascending order.
Ответитьif we pass a sorted list to the function like(if sorted(List):) why it is not searching for the element?
ОтветитьProblem is , you are finding pivot only once , but pivot element will be searched everytime you did not find the required key.
Ответитьthe below code not giving output
Ответить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')
Baap hai bhai tuklu boss programming smajhane mein
I wish my clg teacher were like that but woah toh tatti hai
Sir could you make one about postorder traversal
Ответить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.
ОтветитьSir aap doubts ke liye Jhonny sins ko sb doubts ki le lete ho
ОтветитьYour videos are far better than others
Ответить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
Ответить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 !?
code not working ....
ОтветитьSir we put n value as a User input…,'…
Ответить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")
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
why we did +1 and -1 in the conditional statements??
Ответитьwhy we didnt use i+=1 inside while loop ?
Ответить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)
I really appreciate this video. Thanks a lot
ОтветитьWhat would one's do if you should to find couple numbers in list?
ОтветитьSir this code isn't working for me.
ОтветитьDid he just called me an alien?
Ответить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")
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.
Ответить