

Python Tutorial to learn Python programming with examples
Complete Python Tutorial for Beginners Playlist :
Python Tutorial in Hindi :
Github :-
Editing Monitors :
Check out our website:
Follow Telusko on Twitter:
Follow on Facebook:
Telusko :
Navin Reddy :
Follow Navin Reddy on Instagram:
Subscribe to our other channel:
Navin Reddy :
Telusko Hindi :
Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20
Bala
why so many cuts?
when I try to print the index number of search value, it is giving me the index number after sorting. what should I do to get it according to the original list
Why the error list bound is not callable comming..pls comment sir..
super hard to watch in 480p … can you make fonts bigger in future videos (this comment make no sense in 2020 :))
it doesn't work perfectly if numbers are repeated.
Can anyone post this code with
for loop… Please help…🙏
I have done the binary search before watching your method:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
from time import sleep
list = [4,6,7,55,49,13,46,46,13,48,21,1,8,94]
list = sorted(list)
l_val = list.index(min(list))
u_val = list.index(max(list))
b = int(input('enter the number for search in the list: '))
print('searching…')
sleep(1)
for a in range(u_val):
c = (l_val+u_val)//2
e = list[c]
if e == b:
print('the number is found in the list:',b)
break
elif e < b:
l_val = c
pass
else:
u_val = c
pass
Thanks sir
To correct the issues:
list= [4,8,100,150,177,200,201,205,300,350,400]
n= 400
poss = 0
def search(list,n):
low = 0
upper = len(list)
while low<upper-1:
mid= (low+upper) // 2
if list[low]==n:
return True
if list[mid] == n:
global poss
poss = mid
return True
else:
if list[mid]<n:
low = mid
else:
upper = mid
return False
if search(list,n):
print("found it at: ",poss)
else:
print("not found!")
i got confused on the part of low = mid + 1 and high = mid – 1
If you know the element you're looking for exsists in the list, and you just want to find the index for it. Is there any benefit to binary searching, or can you simply .index()? For example, I have a list of dates in the format '2020-03-01' and I want to find the index of the current days date. Is binnary searching more time efficient? Thanks
Sir please make video on binary search using recursion
Navin, your thought process makes so much sense to me. Thanks for sharing these videos!
I tried this program by converting into for loop but it is not working for starting values because lower bound is increasing so can anybody do this program in for loop and reply to this comment.
What's the complexity of the algorithm?
Can you tell how many total iterations to be done for finding number 45 ??
I expected 3 iterations?
Please reply
why not make the min the mid + 1?
Sir your all video list is good and well arranged but please try to make background font big so that we can see them easily in our Android phones
sir it says error in if search(list, n):
❤
This video is very good. Thanks
Hey, how do I implement binary search in a class?
not working for last element😑
pas = 0
def Binary_search(list,n):
l = 0
u = len(list)
while l <= u:
mid = (l+u)//2
if list[mid] == n:
globals()['pas'] = mid
return True
else:
if list[mid] < n:
l = mid + 1
else:
u = mid – 1
return False
list = [1,2,3,7,9,12,56,100,230,566,767]
n = 767
Binary_search(list,n)
if Binary_search(list,n):
print("found at",pas + 1)
else:
print("not found")
good explanation. one thing pl little bit slow. This is like after a tututorial u r going to catch a train.
excuse me, why should "u" and "l" be assigned to "mid +1" and "mid-1" in else block?
istead of just "mid"
sir can you make it ?
Create a random list of numbers between 0 and 100 with a difference of 2 between
each number. Ask the user for a number between 0 and 100 to check whether their
number is on the list. The program should work like this. The program will half the list of
numbers and see whether the user’s number matches the middle element in the list. If
they do not match, the program will check which half the number lies in, and eliminate
the other half. The search then continues on the remaining half, again checking whether
the middle element in that half is equal to the user’s number. This process keeps on
going until the program finds the users number, or until the size of the subarray is 0,
which means the user’s number isn’t on the list.
Sir you gave this code sorted list but not for unsorted list
I kindly request you to upload the code for unsorted list
hi Navin sir, Thank you for these amazing videos their is one minor mistake
so the correct solution of binary search is :
pos = -1
def search(list, key):
l = 0
h = len(list)-1
for i in range(len(list)):
mid = (l + h) // 2
if list[mid] == key:
globals()['pos'] = mid
return True
else:
if list[mid] < key:
l = mid + 1
else:
h = mid – 1
return False
list = [3, 6, 8, 12, 14, 17, 25, 29, 31, 36, 42, 47, 53, 55, 62]
key = 62
if search(list, key):
print("Element founded at the position ", pos)
else:
print("Element is not found")
o/p:Element founded at the position 14
and
pos = -1
def search(list, key):
l = 0
h = len(list)-1
for i in range(len(list)):
mid = (l + h) // 2
if list[mid] == key:
globals()['pos'] = mid
return True
else:
if list[mid] < key:
l = mid + 1
else:
h = mid – 1
return False
list = [3, 6, 8, 12, 14, 17, 25, 29, 31, 36, 42, 47, 53, 55, 62]
key = 1
if search(list, key):
print("Element founded at the position ", pos)
else:
print("Element is not found")
o/p: Element is not found
and
def search(list, key):
l = 0
h = len(list)-1
for i in range(len(list)):
mid = (l + h) // 2
if list[mid] == key:
globals()['pos'] = mid
return True
else:
if list[mid] < key:
l = mid + 1
else:
h = mid – 1
return False
list = [3, 6, 8, 12, 14, 17, 25, 29, 31, 36, 42, 47, 53, 55, 62]
key = 6666
if search(list, key):
print("Element founded at the position ", pos)
else:
print("Element is not found")
o/p:Element is not found
a=[4,7,8,12,45,99]
n=45
l=0
u=len(a)-1
while l<=u:
m=l+u//2
if a[m]==n:
print('found at '+str(m))
break
else:
if n>a[m]:
l=m
else:
u=m
Can someone explain why pos = -1
it is printing upto 7 index value..when iam trying to search 8 index value it is not printing anything.The loop is repeating infinity times and still searching
Sir sorry to correct you, but this is totally wrong binrary search, it is not good, try it using this list of numbers, and search for 92 =
[4, 7, 8, 12, 24, 43, 48, 68, 79, 89, 92, 130, 150, 162] it returns False, and it is in the list
It's not working for me at all
Hi Naveen…!
For binary search there is one condition. You have to mention that.
"Elements should be sorted"
# Binary Search
class ABC:
def __init__(self, list, num):
self.list = list
self.num = num
def search(self):
l = 0
u = len(self.list) – 1
while l<=u:
mid = (l + u) // 2
if(self.list[mid] == self.num):
globals()['pos'] = mid
return True
else:
if self.list[mid] < self.num:
l = mid+1
else:
u = mid-1
return False
list1 = []
pos = -1
number = int(input("Enter Number of Elements you want: "))
for i in range(number):
print("Enter", (i+1), "element:", end="")
list1.append(int(input()))
print("List is:", end="")
for i in list1:
print(i,end=" ")
search_key = int(input("nEnter Element to search: "))
a1 = ABC(list1, search_key)
if a1.search():
print("Element Found at:", pos+1)
else:
print("Element Not Found!")
Even if..we write l=mid ..then why it's not showing "not found"..🤔 10is still not there in d list.and l=mid . . that's not d issue for not showing d output.