Binary Search - Data Structures & Algorithms Tutorial Python #13

Поделиться
HTML-код
  • Опубликовано: 14 дек 2024

Комментарии • 93

  • @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 4 года назад +22

    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  4 года назад +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 ;)

    • @gauravjoshi3566
      @gauravjoshi3566 Месяц назад

      @@codebasics I wait till the end, just to hear those threats, XD

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

  • @snehakurhade8765
    @snehakurhade8765 3 года назад +22

    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

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

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

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

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

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

  • @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 5 месяцев назад

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

  • @lisamathur9206
    @lisamathur9206 7 месяцев назад

    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)

  • @riteshojha4607
    @riteshojha4607 Год назад

    Best video for binary search

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

  • @globemasterybusinessformul7352
    @globemasterybusinessformul7352 3 года назад

    Your explanation is very clear and practical

  • @andrewmukare3433
    @andrewmukare3433 2 года назад

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

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

  • @abhishekkumaryadav3647
    @abhishekkumaryadav3647 4 года назад

    please continue this series...you explanation is great.

    • @codebasics
      @codebasics  4 года назад

      yes. second video coming up tomorrow on bubble sort

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

  • @anirbanc88
    @anirbanc88 2 года назад

    i love the exercises!

  • @Whitris
    @Whitris 7 месяцев назад

    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

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

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

  • @romanhudec4719
    @romanhudec4719 3 года назад

    Awesome explanation! 👍

  • @bhaskark1485
    @bhaskark1485 2 года назад

    Thank you brother

  • @markmariscal5397
    @markmariscal5397 4 года назад

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

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

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

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

  • @tripathi5174
    @tripathi5174 4 года назад

    Thankyou so much for making it sir.

  • @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 3 месяца назад

    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.

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

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

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

  • @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 10 месяцев назад +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.

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

  • @MaheshKumar-sc9bo
    @MaheshKumar-sc9bo 3 года назад

    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

  • @abhishekdobliyal7178
    @abhishekdobliyal7178 4 года назад

    Sir in recursive function why r_index

  • @lordofperfection5528
    @lordofperfection5528 Месяц назад

    but in recursion the array must be always sorted

  • @akashp4863
    @akashp4863 3 года назад

    Binary search will only work in ordered list right?

  • @sainikhilesh3467
    @sainikhilesh3467 4 года назад

    awesome content. can anyone please share a link to this playlist for all the data structures video you have uploaded sir

    • @codebasics
      @codebasics  4 года назад

      go to youtube and search "Codebasics data structures tutorial playlist"

    • @deepam.g4558
      @deepam.g4558 3 года назад +1

      ruclips.net/p/PLeo1K3hjS3uu_n_a__MI_KktGTLYopZ12

  • @farhathabduljabbar9879
    @farhathabduljabbar9879 4 года назад

    Hi sir how often do you upload?

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

  • @sumitraj5529
    @sumitraj5529 3 года назад

    Can you please share PPT too?

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

  • @meherprudhvi9368
    @meherprudhvi9368 2 года назад

    What if array is not sorted

  • @machinelearninggeek
    @machinelearninggeek 3 года назад

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

  • @udaykiranr1223
    @udaykiranr1223 3 года назад

    it didn't crashed :)

  • @tradingtexi
    @tradingtexi 2 года назад

    Right index at line 54 should be len - 1

  • @bharatkasera8281
    @bharatkasera8281 4 года назад

    Please increase your codes size

    • @codebasics
      @codebasics  4 года назад

      Sure. Took care of it in next video

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

  • @munirajallow
    @munirajallow Год назад

    Thank you sir ❤