Binary Search - Data Structures & Algorithms Tutorial Python #13

  Рет қаралды 79,743

codebasics

codebasics

Күн бұрын

Пікірлер: 93
@codebasics
@codebasics 2 жыл бұрын
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
@priscillacheng44
@priscillacheng44 3 жыл бұрын
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!
@codebasics
@codebasics 3 жыл бұрын
ha ha .. glad you liked them :) my sense of humor is not that great but I am trying to make it better
@2pizen
@2pizen 3 жыл бұрын
@@codebasics i find them extremely amuzing too, they crack me up!! You could improvize further ;)
@gauravjoshi3566
@gauravjoshi3566 19 күн бұрын
@@codebasics I wait till the end, just to hear those threats, XD
@thud93factory51
@thud93factory51 3 жыл бұрын
You are one of the best tutors on youtube for Computer Science, love and respect from Bangladesh.
@akashp4863
@akashp4863 3 жыл бұрын
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
@codebasics
@codebasics 3 жыл бұрын
Akash thanks for this excellent feedback
@pavanpatro5356
@pavanpatro5356 3 жыл бұрын
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.
@lisamathur9206
@lisamathur9206 5 ай бұрын
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)
@tharindusathsara3414
@tharindusathsara3414 Жыл бұрын
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. ❤💖
@sachinladhad5463
@sachinladhad5463 2 жыл бұрын
Dear Sir, You are the best teacher, I am really enjoy videos and learning at the age of 45.
@codebasics
@codebasics 2 жыл бұрын
Great. I wish you all the best
@snehakurhade8765
@snehakurhade8765 3 жыл бұрын
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
@hemachandiran3984
@hemachandiran3984 3 жыл бұрын
def binary_search(list,target): first=0 last=len(list)-1 while first
@Anilsree-06
@Anilsree-06 4 жыл бұрын
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.
@codebasics
@codebasics 4 жыл бұрын
Sure, second algorithm video on bubble sort is coming tomorrow
@winnienakimuli210
@winnienakimuli210 2 жыл бұрын
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!
@zubair4172
@zubair4172 Жыл бұрын
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
@ThaoPhuong-ln9vc
@ThaoPhuong-ln9vc 3 жыл бұрын
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
@kameshparashar
@kameshparashar 4 жыл бұрын
Waiting for this video from a long time ❤️
@codebasics
@codebasics 4 жыл бұрын
Hope you enjoyed it! Second video on bubble sort is coming tomorrow
@globemasterybusinessformul7352
@globemasterybusinessformul7352 3 жыл бұрын
Your explanation is very clear and practical
@codebasics
@codebasics 3 жыл бұрын
thanks
@jykw1717
@jykw1717 3 жыл бұрын
Sir you getting error at the end because you didn't len(numbers_list)-1 for recursive binary search input?
@kkkbuta5
@kkkbuta5 4 ай бұрын
Thank you, I was questioning what went wrong because i could not find fault in the algorithm
@AACREATIONS2000
@AACREATIONS2000 3 жыл бұрын
best and best lecture ever i have seen till now..thank you sir
@codebasics
@codebasics 3 жыл бұрын
I am happy this was helpful to you.
@riteshojha4607
@riteshojha4607 Жыл бұрын
Best video for binary search
@Whitris
@Whitris 6 ай бұрын
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
@abhishekkumaryadav3647
@abhishekkumaryadav3647 4 жыл бұрын
please continue this series...you explanation is great.
@codebasics
@codebasics 4 жыл бұрын
yes. second video coming up tomorrow on bubble sort
@jyotikagrover4527
@jyotikagrover4527 2 жыл бұрын
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
@anirbanc88
@anirbanc88 2 жыл бұрын
i love the exercises!
@rafeeqsyedamjad8875
@rafeeqsyedamjad8875 4 жыл бұрын
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
@ganeshtalari7555
@ganeshtalari7555 4 жыл бұрын
Yes you are right, they are very few videos about data structures using python.
@codebasics
@codebasics 4 жыл бұрын
yes I have resumed the series. Second video on bubble sort is coming tomorrow.
@shubhamkumarshukla6002
@shubhamkumarshukla6002 4 жыл бұрын
@@codebasics sir please make few more videos on graph I think graph in python had no resources on internet please make few more videos
@harris7294
@harris7294 4 жыл бұрын
@@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
@abduchadili5691
@abduchadili5691 Жыл бұрын
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.
@andrewmukare3433
@andrewmukare3433 2 жыл бұрын
I loved this tutorial! You are a great teacher! Thank you!
@sanjuroy8542
@sanjuroy8542 3 жыл бұрын
The Way You teach us its really tremendous ... Love You 3000 sir can you share the ppt!!!
@rohitchitte5614
@rohitchitte5614 3 жыл бұрын
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 .
@zack176
@zack176 Жыл бұрын
Is this for exercise
@markmariscal5397
@markmariscal5397 4 жыл бұрын
Yes!!!! My fav algorithm quick run time yet simple implementation
@codebasics
@codebasics 4 жыл бұрын
Glad you like it!
@rogerthat7190
@rogerthat7190 Жыл бұрын
Thank you so much sir
@jeenieb6255
@jeenieb6255 2 ай бұрын
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.
@bhaskark1485
@bhaskark1485 2 жыл бұрын
Thank you brother
@romanhudec4719
@romanhudec4719 3 жыл бұрын
Awesome explanation! 👍
@codebasics
@codebasics 3 жыл бұрын
Glad you liked it
@mount8857
@mount8857 2 жыл бұрын
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😂
@hellohello1006
@hellohello1006 3 жыл бұрын
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
@NaveenKinnal-k1e
@NaveenKinnal-k1e Жыл бұрын
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 :)
@tripathi5174
@tripathi5174 4 жыл бұрын
Thankyou so much for making it sir.
@codebasics
@codebasics 4 жыл бұрын
It's my pleasure
@niyatham8525
@niyatham8525 2 жыл бұрын
do you assume the list is sorted? and then perform a binary search?
@codebasics
@codebasics 2 жыл бұрын
yes binary search works only on sorted list. If list is not sorted you need to sort it first
@joxa6119
@joxa6119 Жыл бұрын
Question, so by using this algorithm, we assumed that the list numbers is in sorted right? Otherwise could it work?
@alexlu2
@alexlu2 9 ай бұрын
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.
@lordofperfection5528
@lordofperfection5528 21 күн бұрын
but in recursion the array must be always sorted
@atleefernandes264
@atleefernandes264 2 жыл бұрын
@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?
@MaheshKumar-sc9bo
@MaheshKumar-sc9bo 2 жыл бұрын
thank u sir
@meralmaradia4774
@meralmaradia4774 2 жыл бұрын
Hello Sir, can you please create a video developing of project using only DSA ?
@abhishekdobliyal7178
@abhishekdobliyal7178 4 жыл бұрын
Sir in recursive function why r_index
@ayanchowdhury6732
@ayanchowdhury6732 4 жыл бұрын
Please make a series on NLP
@codebasics
@codebasics 4 жыл бұрын
yes that is in my plans
@ayanchowdhury6732
@ayanchowdhury6732 4 жыл бұрын
@@codebasics thanks...waiting eagerly
@meherprudhvi9368
@meherprudhvi9368 2 жыл бұрын
What if array is not sorted
@pankajbhatt7373
@pankajbhatt7373 2 жыл бұрын
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 ')
@Alex-xh2tb
@Alex-xh2tb 2 жыл бұрын
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!
@akashp4863
@akashp4863 3 жыл бұрын
Binary search will only work in ordered list right?
@codebasics
@codebasics 3 жыл бұрын
Yes
@sumitraj5529
@sumitraj5529 3 жыл бұрын
Can you please share PPT too?
@piyushjain1237
@piyushjain1237 4 жыл бұрын
Bhai mein coding beginner hu toh kya mujhe C language sikhni chaiye kya
@codebasics
@codebasics 4 жыл бұрын
Not necessary. Start with python directly.
@piyushjain1237
@piyushjain1237 4 жыл бұрын
@@codebasics Thank you sir
@machinelearninggeek
@machinelearninggeek 3 жыл бұрын
y not use sort function inside all function to make better code
@farhathabduljabbar9879
@farhathabduljabbar9879 4 жыл бұрын
Hi sir how often do you upload?
@codebasics
@codebasics 4 жыл бұрын
few videos a week
@sainikhilesh3467
@sainikhilesh3467 3 жыл бұрын
awesome content. can anyone please share a link to this playlist for all the data structures video you have uploaded sir
@codebasics
@codebasics 3 жыл бұрын
go to youtube and search "Codebasics data structures tutorial playlist"
@deepam.g4558
@deepam.g4558 3 жыл бұрын
kzbin.info/aero/PLeo1K3hjS3uu_n_a__MI_KktGTLYopZ12
@udaykiranr1223
@udaykiranr1223 3 жыл бұрын
it didn't crashed :)
@DiaaHaresYusf
@DiaaHaresYusf 2 жыл бұрын
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)
@bharatkasera8281
@bharatkasera8281 4 жыл бұрын
Please increase your codes size
@codebasics
@codebasics 4 жыл бұрын
Sure. Took care of it in next video
@tradingtexi
@tradingtexi 2 жыл бұрын
Right index at line 54 should be len - 1
@ganeshtalari7555
@ganeshtalari7555 4 жыл бұрын
Hello sir, I request you to use light mode than dark mode because dark mode is too dull to watch.
@aaravsajialoysiussaji110
@aaravsajialoysiussaji110 4 жыл бұрын
No,dark mode gives thrill
@codebasics
@codebasics 4 жыл бұрын
Point taken.
@ganeshtalari7555
@ganeshtalari7555 4 жыл бұрын
@@codebasics thank you sir
@munirajallow
@munirajallow Жыл бұрын
Thank you sir ❤
Bubble Sort - Data Structures & Algorithms Tutorial Python #14
15:09
Which team will win? Team Joy or Team Gumball?! 🤔
00:29
BigSchool
Рет қаралды 15 МЛН
Из какого города смотришь? 😃
00:34
МЯТНАЯ ФАНТА
Рет қаралды 1,3 МЛН
amazing#devil #lilith #funny #shorts
00:15
Devil Lilith
Рет қаралды 18 МЛН
Osman Kalyoncu Sonu Üzücü Saddest Videos Dream Engine 275 #shorts
00:29
Linear & Binary Search Code | Big O Notation
19:31
Telusko
Рет қаралды 35 М.
Hash Table - Data Structures & Algorithms Tutorials In Python #5
17:52
Binary Search - Leetcode 704 - Python
9:40
NeetCode
Рет қаралды 166 М.
How I Got Good at Coding Interviews
6:29
NeetCode
Рет қаралды 1,7 МЛН
Quick Sort - Data Structures & Algorithms Tutorial Python #15
31:17
Binary Tree Algorithms for Technical Interviews - Full Course
1:48:53
freeCodeCamp.org
Рет қаралды 730 М.
Binary Search Algorithm - Computerphile
18:34
Computerphile
Рет қаралды 163 М.
Which team will win? Team Joy or Team Gumball?! 🤔
00:29
BigSchool
Рет қаралды 15 МЛН