Binary Search - Data Structures & Algorithms Tutorial Python #13

Поділитися
Вставка
  • Опубліковано 18 вер 2024

КОМЕНТАРІ • 91

  • @codebasics
    @codebasics  2 роки тому +1

    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 роки тому +18

    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 роки тому +5

      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 ;)

  • @akashp4863
    @akashp4863 3 роки тому +2

    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 роки тому +1

      Akash thanks for this excellent feedback

  • @thud93factory51
    @thud93factory51 3 роки тому +1

    You are one of the best tutors on youtube for Computer Science, love and respect from Bangladesh.

  • @pavanpatro5356
    @pavanpatro5356 3 роки тому +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.

  • @tharindusathsara3414
    @tharindusathsara3414 Рік тому +1

    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. ❤💖

  • @snehakurhade8765
    @snehakurhade8765 3 роки тому +21

    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 роки тому +1

      def binary_search(list,target):
      first=0
      last=len(list)-1

      while first

  • @winnienakimuli210
    @winnienakimuli210 2 роки тому +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!

  • @jykw1717
    @jykw1717 3 роки тому +6

    Sir you getting error at the end because you didn't len(numbers_list)-1 for recursive binary search input?

    • @kkkbuta5
      @kkkbuta5 2 місяці тому

      Thank you, I was questioning what went wrong because i could not find fault in the algorithm

  • @zubair4172
    @zubair4172 Рік тому +2

    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

  • @kameshparashar
    @kameshparashar 4 роки тому +3

    Waiting for this video from a long time ❤️

    • @codebasics
      @codebasics  4 роки тому +3

      Hope you enjoyed it! Second video on bubble sort is coming tomorrow

  • @anilvenkatesh6
    @anilvenkatesh6 4 роки тому +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.

    • @codebasics
      @codebasics  4 роки тому

      Sure, second algorithm video on bubble sort is coming tomorrow

  • @ThaoPhuong-ln9vc
    @ThaoPhuong-ln9vc 2 роки тому

    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

  • @Whitris
    @Whitris 4 місяці тому

    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

  • @lisamathur9206
    @lisamathur9206 4 місяці тому

    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)

  • @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

  • @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.

  • @rafeeqsyedamjad8875
    @rafeeqsyedamjad8875 4 роки тому +6

    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 роки тому +1

      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 3 роки тому

      @@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

  • @globemasterybusinessformul7352
    @globemasterybusinessformul7352 3 роки тому

    Your explanation is very clear and practical

  • @riteshojha4607
    @riteshojha4607 Рік тому

    Best video for 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

  • @andrewmukare3433
    @andrewmukare3433 2 роки тому

    I loved this tutorial! You are a great teacher! Thank you!

  • @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.

  • @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

  • @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

  • @munirajallow
    @munirajallow Рік тому

    Thank you sir ❤

  • @rogerthat7190
    @rogerthat7190 Рік тому

    Thank you so much sir

  • @sanjuroy8542
    @sanjuroy8542 3 роки тому

    The Way You teach us its really tremendous ... Love You 3000
    sir can you share the ppt!!!

  • @anirbanc88
    @anirbanc88 2 роки тому

    i love the exercises!

  • @markmariscal5397
    @markmariscal5397 4 роки тому

    Yes!!!! My fav algorithm quick run time yet simple implementation

  • @romanhudec4719
    @romanhudec4719 3 роки тому

    Awesome explanation! 👍

  • @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😂

  • @jeenieb6255
    @jeenieb6255 22 дні тому

    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.

  • @rohitchitte5614
    @rohitchitte5614 2 роки тому

    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

  • @tripathi5174
    @tripathi5174 4 роки тому

    Thankyou so much for making it sir.

  • @joxa6119
    @joxa6119 10 місяців тому

    Question, so by using this algorithm, we assumed that the list numbers is in sorted right? Otherwise could it work?

    • @alexlu2
      @alexlu2 7 місяців тому +1

      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.

  • @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?

  • @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

  • @abhishekdobliyal7178
    @abhishekdobliyal7178 4 роки тому

    Sir in recursive function why r_index

  • @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 :)

  • @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 ?

  • @ayanchowdhury6732
    @ayanchowdhury6732 4 роки тому

    Please make a series on NLP

  • @Alex-xh2tb
    @Alex-xh2tb Рік тому

    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!

  • @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 ')

  • @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 роки тому +1

      ua-cam.com/play/PLeo1K3hjS3uu_n_a__MI_KktGTLYopZ12.html

  • @meherprudhvi9368
    @meherprudhvi9368 2 роки тому

    What if array is not sorted

  • @akashp4863
    @akashp4863 3 роки тому

    Binary search will only work in ordered list right?

  • @machinelearninggeek
    @machinelearninggeek 3 роки тому

    y not use sort function inside all function to make better code

  • @sumitraj5529
    @sumitraj5529 3 роки тому

    Can you please share PPT too?

  • @piyushjain1237
    @piyushjain1237 4 роки тому +1

    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

  • @farhathabduljabbar9879
    @farhathabduljabbar9879 4 роки тому

    Hi sir how often do you upload?

  • @udaykiranr1223
    @udaykiranr1223 3 роки тому

    it didn't crashed :)

  • @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.

  • @DiaaHaresYusf
    @DiaaHaresYusf Рік тому

    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)

  • @bhaskark1485
    @bhaskark1485 2 роки тому

    Thank you brother