Do you want to learn python from me with a lot of interactive quizzes, and exercises? Here is my project-based python learning course: codebasics.io/courses/python-for-beginner-and-intermediate-learners
I love your witty quips! "If you click 'solution' too early, you will get a computer virus" lol I find myself looking forward to them every video. Thanks for making this!
underrated video. you're the best teacher for me in Data structures and Algorithm. the way you think is the way i also think so i end up with doubts like why and hows, which you clear at the exact same time. Thank you
Firstly, simplistic yet awesome!! explanation.. A scenario expressing out of curiosity (more from performance perspective) : "Let's assume a scenario, where the input list of numbers doesn't follow any particular order i.e. ascending/descending.. then sorting of the list first, prior to performing the binary search enhances the code performance.
Really very happy about finding such an obvious and understood funny video series about data structures and algorithms. Everything is 100% clear with deeply explained theories and well-understood practicals. Also, the exercise series with the videos are highly appreciated. Dear sir thank you so much for the fantastic video series. ❤💖
The recursion code errors because when you call the function in the main, the right index is provided as len(number_list) which is 8 in your case. Instead the right index should be initialized with (len(number_list)-1). Thus your right index now correctly points to location 7 which consists of value 67
Thank you so much Sir for this series of videos, they have helped a lot being a newbie at these topics, and the debugging tip is everything, thank you!
Hello @codebasics thanks for very good explaination but i want to add one thing in "binary_search_recursive" function you don't need to handle if mid_index >= len(numbers_list) or mid_index < 0: return -1 just provide right_index correct like you are providing right_index = len(numbers_list) but it should be len(numbers_list)-1
Thank you @codebasics, requesting you to kindly continue this series of Data Structures and Algorithms in Python sir. This is really good and very helpful for beginners who aim to go from zero to hero. Thank you once again sir.
Need help understanding big O: 1. What is the average time complexity of the 2nd exercise solution? My answer is O(log n + left + right), where left + right
Kudos for your courses! Just a thing: it would be better to use time.perf_counter() inside the time_it decorator, instead of time.time(). You would get a much better resolution, avoiding the "0.0 mil sec" measurement for the binary search
Sir plz continue this series ds and algo with python online we have fewer resources to follow ds and algo with python it will be really helpful to crack product based companies for data science. upload videos related to this as much as possible
Very good series, reminded me my college Data Structures & Algorithms, we used Pascal language back then. The only issue is when you show PyCharm it is very blurry and a bit eye hurting.
In FIND_ALL_OCCURANCES exercise if you remove "else: break" from the code then it'll print all the occurances regardless of where the element is present
Hello Sir! This is my take on your exercise, do you think this is too complicated?: def binary_search_multi(num_list, num_to_find, left, right, occurences=[]): if right < left: return mid_index = (left + right) // 2 if mid_index >= len(num_list) or mid_index < 0: return mid_element = num_list[mid_index] if mid_element == num_to_find: if str(mid_index) not in occurences: occurences.append(str(mid_index)) binary_search_multi(num_list, num_to_find, left, right-1) binary_search_multi(num_list, num_to_find, left+1, right) elif mid_element < num_to_find: left += 1 binary_search_multi(num_list, num_to_find, left, right) else: right -= 1 binary_search_multi(num_list, num_to_find, left, right) return occurences
Hello there :) A great tutor indeed! In this video, the binary search algorithm only works on integer lists. Could you improvise it to work on strings too. I got you an assignment too😂
The recursive function is repeatedly throwing an error of ' name binary_search_recursive is not defined'. Could you please guide me on how to resolve this? I have a sorted array in place & correct indentation as well.
Yes, we are assuming that the list numbers is in sorted order. It would not work otherwise because if the list was not in order, you cannot guarantee that the numbers on the left or right is definitely bigger or smaller than your target number.
@codebasics How to use binary sort to search a key vakue pair in a list of dictionaries? Its possible through Linear Search however if I want to make it search faster what do I use?
Had you put the right index as "len(numbers_list) - 1" in line 51 of the recursive program, you would not have got the error "list out of range" error :)
if you debug this code , you will see control is moving to else condtion when list remain [16,17] , which should not happen. can anyone tell me why ? def binarySearch(myList,element): '''This will take only sorted list''' print(myList) mid = len(myList)//2 print(mid) if mid==0 : return -1 elif element == myList[mid]: return mid else: while element < myList[mid]: binarySearch(myList[:mid], element) while element > myList[mid]: binarySearch(myList[mid:], element) if __name__== '__main__': myList = [11,12,13,14,15,16,17] print('list on which item is serched',myList,end=' '*3) element = 17 print(f'Element to be searched {element}',end=' '*3)
pos = binarySearch(myList,element) print(f'{element} is present at {pos} position ')
this is my code before seeing yours , thanks bro def binary_search(number_list , find_number , right = None, left = None): if right is None: r = len(number_list) - 1 #this will be used only in the first call else: r = right if left is None: l = 0 #this will be used ony in the first call else: l = left mid =int(l + (r-l)/2) #base condition if number_list[mid] == find_number: return mid #because in this cas mid is the index you are searching for. if r < l: return -1 #means that this number does not exist, #itteration behavior if number_list[mid] < find_number: l = mid + 1 return binary_search(number_list , find_number , r , l ) elif number_list[mid] > find_number: r = mid - 1 return binary_search(number_list , find_number , r , l)
Do you want to learn python from me with a lot of interactive quizzes, and exercises? Here is my project-based python learning course: codebasics.io/courses/python-for-beginner-and-intermediate-learners
I love your witty quips! "If you click 'solution' too early, you will get a computer virus" lol I find myself looking forward to them every video. Thanks for making this!
ha ha .. glad you liked them :) my sense of humor is not that great but I am trying to make it better
@@codebasics i find them extremely amuzing too, they crack me up!! You could improvize further ;)
underrated video. you're the best teacher for me in Data structures and Algorithm. the way you think is the way i also think so i end up with doubts like why and hows, which you clear at the exact same time. Thank you
Akash thanks for this excellent feedback
You are one of the best tutors on youtube for Computer Science, love and respect from Bangladesh.
Firstly, simplistic yet awesome!! explanation..
A scenario expressing out of curiosity (more from performance perspective) : "Let's assume a scenario, where the input list of numbers doesn't follow any particular order i.e. ascending/descending.. then sorting of the list first, prior to performing the binary search enhances the code performance.
Really very happy about finding such an obvious and understood funny video series about data structures and algorithms. Everything is 100% clear with deeply explained theories and well-understood practicals. Also, the exercise series with the videos are highly appreciated. Dear sir thank you so much for the fantastic video series. ❤💖
The recursion code errors because when you call the function in the main, the right index is provided as len(number_list) which is 8 in your case.
Instead the right index should be initialized with (len(number_list)-1). Thus your right index now correctly points to location 7 which consists of value 67
def binary_search(list,target):
first=0
last=len(list)-1
while first
Thank you so much Sir for this series of videos, they have helped a lot being a newbie at these topics, and the debugging tip is everything, thank you!
Sir you getting error at the end because you didn't len(numbers_list)-1 for recursive binary search input?
Thank you, I was questioning what went wrong because i could not find fault in the algorithm
Hello @codebasics thanks for very good explaination but i want to add one thing in "binary_search_recursive" function you don't need to handle
if mid_index >= len(numbers_list) or mid_index < 0:
return -1
just provide right_index correct like you are providing right_index = len(numbers_list) but it should be len(numbers_list)-1
Waiting for this video from a long time ❤️
Hope you enjoyed it! Second video on bubble sort is coming tomorrow
Thank you @codebasics, requesting you to kindly continue this series of Data Structures and Algorithms in Python sir. This is really good and very helpful for beginners who aim to go from zero to hero. Thank you once again sir.
Sure, second algorithm video on bubble sort is coming tomorrow
Need help understanding big O:
1. What is the average time complexity of the 2nd exercise solution?
My answer is O(log n + left + right), where left + right
Kudos for your courses! Just a thing: it would be better to use time.perf_counter() inside the time_it decorator, instead of time.time(). You would get a much better resolution, avoiding the "0.0 mil sec" measurement for the binary search
Another approach to solve with bin_serarch_rec -
def bin_search_rec(numbers, key):
l = 0
u = len(numbers) - 1
mid = (u+l)//2
if numbers == []:
return False
if numbers[mid] == key:
return True
if numbers[mid] > key:
return bin_search_rec(numbers[0:mid],key)
if numbers[mid] < key:
return bin_search_rec(numbers[mid+1 : ],key)
Dear Sir,
You are the best teacher, I am really enjoy videos and learning at the age of 45.
Great. I wish you all the best
best and best lecture ever i have seen till now..thank you sir
I am happy this was helpful to you.
Sir plz continue this series ds and algo with python
online we have fewer resources to follow ds and algo with python it will be really helpful to crack product based companies for data science. upload videos related to this as much as possible
Yes you are right, they are very few videos about data structures using python.
yes I have resumed the series. Second video on bubble sort is coming tomorrow.
@@codebasics sir please make few more videos on graph I think graph in python had no resources on internet please make few more videos
@@codebasics sir that error was about index out of limit bcoz initially you gave right index as len(list) it should have been len(list) - 1
Your explanation is very clear and practical
thanks
Best video for binary search
please continue this series...you explanation is great.
yes. second video coming up tomorrow on bubble sort
I loved this tutorial! You are a great teacher! Thank you!
Very good series, reminded me my college Data Structures & Algorithms, we used Pascal language back then. The only issue is when you show PyCharm it is very blurry and a bit eye hurting.
In FIND_ALL_OCCURANCES exercise if you remove "else:
break"
from the code then it'll print all the occurances regardless of where the element is present
Hello Sir! This is my take on your exercise, do you think this is too complicated?:
def binary_search_multi(num_list, num_to_find, left, right, occurences=[]):
if right < left:
return
mid_index = (left + right) // 2
if mid_index >= len(num_list) or mid_index < 0:
return
mid_element = num_list[mid_index]
if mid_element == num_to_find:
if str(mid_index) not in occurences:
occurences.append(str(mid_index))
binary_search_multi(num_list, num_to_find, left, right-1)
binary_search_multi(num_list, num_to_find, left+1, right)
elif mid_element < num_to_find:
left += 1
binary_search_multi(num_list, num_to_find, left, right)
else:
right -= 1
binary_search_multi(num_list, num_to_find, left, right)
return occurences
Thank you sir ❤
Thank you so much sir
The Way You teach us its really tremendous ... Love You 3000
sir can you share the ppt!!!
i love the exercises!
Yes!!!! My fav algorithm quick run time yet simple implementation
Glad you like it!
Awesome explanation! 👍
Glad you liked it
Hello there :) A great tutor indeed! In this video, the binary search algorithm only works on integer lists. Could you improvise it to work on strings too. I got you an assignment too😂
The recursive function is repeatedly throwing an error of ' name binary_search_recursive is not defined'. Could you please guide me on how to resolve this? I have a sorted array in place & correct indentation as well.
def recursive_binary_search(lis,key,start,end):
if start middle:
start = middle + 1
return recursive_binary_search(lis,key,start,end)
else :
end = middle - 1
return recursive_binary_search(lis,key,start,end)
# this approach worker for me .
Is this for exercise
Thankyou so much for making it sir.
It's my pleasure
Question, so by using this algorithm, we assumed that the list numbers is in sorted right? Otherwise could it work?
Yes, we are assuming that the list numbers is in sorted order. It would not work otherwise because if the list was not in order, you cannot guarantee that the numbers on the left or right is definitely bigger or smaller than your target number.
@codebasics How to use binary sort to search a key vakue pair in a list of dictionaries? Its possible through Linear Search however if I want to make it search faster what do I use?
do you assume the list is sorted? and then perform a binary search?
yes binary search works only on sorted list. If list is not sorted you need to sort it first
Sir in recursive function why r_index
Had you put the right index as "len(numbers_list) - 1" in line 51 of the recursive program, you would not have got the error "list out of range" error :)
thank u sir
Hello Sir, can you please create a video developing of project using only DSA ?
Please make a series on NLP
yes that is in my plans
@@codebasics thanks...waiting eagerly
9:50 "you want to find a mid number so my mid number is ... this! right??"
where is any tiny explanation for this ???
Thank you!
if you debug this code , you will see control is moving to else condtion when list remain [16,17] , which should not happen. can anyone tell me why ?
def binarySearch(myList,element):
'''This will take only sorted list'''
print(myList)
mid = len(myList)//2
print(mid)
if mid==0 :
return -1
elif element == myList[mid]:
return mid
else:
while element < myList[mid]:
binarySearch(myList[:mid], element)
while element > myList[mid]:
binarySearch(myList[mid:], element)
if __name__== '__main__':
myList = [11,12,13,14,15,16,17]
print('list on which item is serched',myList,end='
'*3)
element = 17
print(f'Element to be searched {element}',end='
'*3)
pos = binarySearch(myList,element)
print(f'{element} is present at {pos} position ')
awesome content. can anyone please share a link to this playlist for all the data structures video you have uploaded sir
go to youtube and search "Codebasics data structures tutorial playlist"
ua-cam.com/play/PLeo1K3hjS3uu_n_a__MI_KktGTLYopZ12.html
What if array is not sorted
Binary search will only work in ordered list right?
Yes
y not use sort function inside all function to make better code
Can you please share PPT too?
Bhai mein coding beginner hu toh kya mujhe C language sikhni chaiye kya
Not necessary. Start with python directly.
@@codebasics Thank you sir
Hi sir how often do you upload?
few videos a week
it didn't crashed :)
Please increase your codes size
Sure. Took care of it in next video
Right index at line 54 should be len - 1
Hello sir,
I request you to use light mode than dark mode because dark mode is too dull to watch.
No,dark mode gives thrill
Point taken.
@@codebasics thank you sir
this is my code before seeing yours , thanks bro
def binary_search(number_list , find_number , right = None, left = None):
if right is None:
r = len(number_list) - 1 #this will be used only in the first call
else:
r = right
if left is None:
l = 0 #this will be used ony in the first call
else:
l = left
mid =int(l + (r-l)/2)
#base condition
if number_list[mid] == find_number:
return mid #because in this cas mid is the index you are searching for.
if r < l:
return -1 #means that this number does not exist,
#itteration behavior
if number_list[mid] < find_number:
l = mid + 1
return binary_search(number_list , find_number , r , l )
elif number_list[mid] > find_number:
r = mid - 1
return binary_search(number_list , find_number , r , l)
Thank you brother