2.6.2 Binary Search Recursive Method

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ม.ค. 2025

ความคิดเห็น • 160

  • @atulgunjal6627
    @atulgunjal6627 4 ปีที่แล้ว +52

    Your mastery in converting complicated algorithmic steps into the simple stages is impressive.

  • @mohammadfaisal3649
    @mohammadfaisal3649 ปีที่แล้ว +57

    i wish there was a coding part also. i Have NEVER seen a teacher with this level of understanding in data structures.

  • @nitiKT
    @nitiKT 2 ปีที่แล้ว +15

    Bari Sir, hats off to you. I brag to my friends that I was your student and thought me DSA during my engineering and every time I have an interview I make sure I go through some of your videos..

  • @mansoorkhattak5072
    @mansoorkhattak5072 5 ปีที่แล้ว +129

    Sir you are hero , superb !! Allah lambi zindagi de :)

  • @preethamm.n1161
    @preethamm.n1161 5 ปีที่แล้ว +49

    Sir, u are my hero sir
    👑God of algorithms 👑
    😇🌞😎💻
    "Simply in the nptel outdated teachers are teaching the same concept for hours together
    And u are awesome sir without missing out any of the concepts u are teaching better than them sir"
    As enstien said "if u can't explain in simple ,u have to understand well enough"
    For the above qoute Ur best example sir
    Thank u sir it's helping me a lot
    God bless u sir

    • @elhamatokhi
      @elhamatokhi หลายเดือนก่อน

      yes, he explains very well. but that doesn't necessarily make him god of Algorithms 😅

  • @jessesinger4790
    @jessesinger4790 5 ปีที่แล้ว +14

    31/83 videos in, bought your course on data structures. Maybe jumping ahead of myself, but you're amazing at explaining this.

  • @mohamedatef3526
    @mohamedatef3526 3 ปีที่แล้ว +9

    This is, by far the best tutorial for learning binary search I've ever seen.
    Many people have sufficient knowledge and skills and they try passing them to others
    but a few of them can deliver them in a convenient method like Abdul Bari Does.

  • @abhilashpatel3036
    @abhilashpatel3036 4 ปีที่แล้ว +15

    Being from non cs background, it was very difficult for me to find time complexity of a algorithm. Your recurrence relation series has helped me alot.

  • @mustafamahmoudahmed1778
    @mustafamahmoudahmed1778 2 ปีที่แล้ว +7

    Thanks for this helpful playlist, you are the best.
    I tried to implement the algorithm as it is in the video, it didn't work except for values already in the array.
    My recommendation:
    1- remove the if block
    2- Modify the else block to be "if (l h"
    Here is my implementation in C++:
    int BinSearch(int l, int h, int key,vector arr) {
    int mid;
    if (l

  • @protek8
    @protek8 4 ปีที่แล้ว +5

    I never understood recursion, but after watching this video I know all of it
    thanks, Abdul sir!

  • @backpropagated
    @backpropagated 3 ปีที่แล้ว +1

    Thank you for these videos Abdul Bari. I normally struggle to learn from videos, but your concisesness helps so much. I know that my focus won't be wasted, so I can listen intently for the full video.

  • @Swansylinks
    @Swansylinks 4 หลายเดือนก่อน +1

    You're so unique, I failed algorithm because I didn't understand it when my lecturer taught me. I am sure I will scatter my Saturday's exam curtesy of you Dr Abdul Bari.

  • @StefanoCocomazzi93
    @StefanoCocomazzi93 6 ปีที่แล้ว +8

    I can only shay Thank You. I Just passed my algorithm exam with honors.
    Thank you, thank you and again thank you

  • @subramaniyanvg6367
    @subramaniyanvg6367 4 ปีที่แล้ว +10

    Sir you should be guiding students for companies like google and amazon. Also please write a book on Data Structures definitely I will buy.

  • @AbdullahSiddiqui949
    @AbdullahSiddiqui949 5 ปีที่แล้ว +6

    now i am clear you teach it better then , even our university teacher.

    • @Farahat1234
      @Farahat1234 5 ปีที่แล้ว

      From which university you are?

  • @saran5671
    @saran5671 2 ปีที่แล้ว +6

    There is a slight mistake in your algorithm if(l==h) will throw you a stackoverflow error instead you should use if(h

  • @eva42sh
    @eva42sh 2 ปีที่แล้ว +1

    cannot express my gratitude. Thank you Mr Abdul - all of your videos are priceless! you're a legend. Thank you for helping to improve

  • @gayatri263
    @gayatri263 3 ปีที่แล้ว +3

    God bless you and your loved ones..... Keep rising... 🥰😘

  • @weixinwang52
    @weixinwang52 2 ปีที่แล้ว +2

    That's the thought I was looking for, thanks

  • @eva42sh
    @eva42sh 2 ปีที่แล้ว +1

    Thank you Mr Abdul - all of your videos are priceless! you're a legend. Thank you for helping to improve

  • @intuitivej9327
    @intuitivej9327 3 ปีที่แล้ว +4

    I can finally write the code after this lecture series! Thank you from South Korea.

  • @devesh1697
    @devesh1697 6 ปีที่แล้ว +44

    I know my DAA is in safe hands now.

    • @amuzak9063
      @amuzak9063 3 ปีที่แล้ว +2

      and 2 years later, I do too

    • @yogeshwarsingh6151
      @yogeshwarsingh6151 3 ปีที่แล้ว

      @@amuzak9063 lmao Samw😂

    • @bageshwardham-1M
      @bageshwardham-1M 3 ปีที่แล้ว +1

      DAA or DSA??😅

    • @ApniKaksha_bonded
      @ApniKaksha_bonded 8 หลายเดือนก่อน

      ​@@bageshwardham-1Msame question

    • @satyarth5314
      @satyarth5314 10 วันที่ผ่านมา

      and 6 years later, I do too

  • @dmsnm
    @dmsnm 4 ปีที่แล้ว +22

    One question: Aren't we suppose to return 0 for successful events and -1,1, etc for unsuccessful events?
    Great lecture series btw.

    • @66renegade66
      @66renegade66 4 ปีที่แล้ว +16

      Correct. The tutor here has array indices starting at 1. So function can return 0 if key not found.
      Most languages have array indices starting at 0, so return -1 if not found.

  • @luckywhite7241
    @luckywhite7241 ปีที่แล้ว

    Great to come across your clips on algorithm, you lecture just rolled away huge pressure off my neck. Thanks Sir

  • @zureka3242
    @zureka3242 4 ปีที่แล้ว +2

    Sir in your recursive code, you didn't give a condition for when the element can't be found, i.e. you have to give a condition that when l>h return -1. Other than that amazing lectures

    • @vineetwidhani8513
      @vineetwidhani8513 3 ปีที่แล้ว

      The first condition where l==h is when the algo will return 0 when an element is not found, in case the element is not found, before l becoming greater than h , it first becomes equal to l.

  • @NikhilSrivastava-t6w
    @NikhilSrivastava-t6w 18 วันที่ผ่านมา

    That such teacher make our future bright with their soft english . That make better understanding to about problem

  • @initalAS
    @initalAS 6 ปีที่แล้ว +14

    only one word sir """SUPERRB"""

  • @khale-d
    @khale-d ปีที่แล้ว +1

    this also is a good algorithm:
    def RBinarySearch(arr, value,l = 0,h = 0):
    if(l>h):
    return -1
    mid = int((l+h) /2)
    if(arr[mid] == value):
    return mid
    elif (value > arr[mid]):
    l = mid+1
    else :
    h = mid-1
    return RBinarySearch(arr,value,l,h)
    arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]
    print(RBinarySearch(arr,25,0,len(arr)-1))

  • @FELIPE-vj7vd
    @FELIPE-vj7vd 2 ปีที่แล้ว +1

    I have just one thing to say: Thank you!

  • @anthatikalyan4995
    @anthatikalyan4995 5 ปีที่แล้ว +2

    I love your way of delivering... Awesome

  • @AbhishekYadav-y6r
    @AbhishekYadav-y6r 2 หลายเดือนก่อน +1

    Thank you sir for your every lecture

  • @practicemail3227
    @practicemail3227 ปีที่แล้ว +6

    I'm a non-cs background student and even i can understand such simple explanation. Hats off to you sir for your contribution sir.

  • @chaulethanh752
    @chaulethanh752 3 ปีที่แล้ว

    I can't see the method where you find the first occurrence of the element to look for and what if your array r

  • @akashbose5228
    @akashbose5228 5 ปีที่แล้ว +3

    Sir
    instead of that if l==h block
    if we just check l>h and return wouldn't it still do the same considering we are already comparing with a[mid] later?
    Thank you

  • @colemannelson5392
    @colemannelson5392 3 ปีที่แล้ว

    Made this very easy to understand, divide and conquer!

  • @saivishwanath958
    @saivishwanath958 5 ปีที่แล้ว +1

    Great content . Quick correction : The recurrence relation is 2 * T(n/2) +1

  • @tubex1300
    @tubex1300 2 ปีที่แล้ว +1

    Master what kinds of problems we can divide? like in case of the binary search above?

  • @vijaypalmanit
    @vijaypalmanit 6 ปีที่แล้ว

    super sir, your videos are really very helpful, you make the subject simple so that even a new student can understand, I am able to write programs watching your videos, thank you so much.

  • @codingvibesofficial
    @codingvibesofficial 2 ปีที่แล้ว +1

    Why are we using return in if else block..instead we can avoid return in if else block
    I think return mid is sufficient

  • @anbarasanv5625
    @anbarasanv5625 6 ปีที่แล้ว +6

    passing arguments (l,h,key,Arr).. I think array missing in the passed arguments sir

    • @anirbanmukherjee5591
      @anirbanmukherjee5591 6 ปีที่แล้ว

      Call a constructor.

    • @vineetwidhani8513
      @vineetwidhani8513 3 ปีที่แล้ว

      In such cases array is always declared as global variable. You can also pass it as argument but that would take more system memory.

    • @imgeek4282
      @imgeek4282 2 ปีที่แล้ว

      @@vineetwidhani8513 Pass the reference then?

  • @CLG111
    @CLG111 2 หลายเดือนก่อน

    I've had two algorithms courses one as an undergrad and also one as a master's student and I was always kind of iffy about my understanding but now I understand and my learning is complete thanks to Professor Bari.

  • @josephsebastian1244
    @josephsebastian1244 3 ปีที่แล้ว

    bro its good.my friend suggest u and i was lucky i got u

  • @jayadevpulaparty1341
    @jayadevpulaparty1341 2 ปีที่แล้ว +1

    One thing i observed is that if we take an even sized array, we are getting tree of depth 5 instead of log(16)= 4. Please let me know if my observation is valid

  • @AmulyaKishoo
    @AmulyaKishoo 5 ปีที่แล้ว +1

    Binary Search has only Divide strategy and not conquer strategy. So, does it come under Divide & Conquer ?

  • @thenerdycoder07
    @thenerdycoder07 4 ปีที่แล้ว

    Best Ada teacher♥️♥️♥️♥️

  • @amberchawla7930
    @amberchawla7930 3 ปีที่แล้ว +1

    Sir i dont think l > h , case is covered which actually could occur

  • @saifurrahman3961
    @saifurrahman3961 4 ปีที่แล้ว +2

    I am facing a problem with this algorithm, if the key is not found then the function is not returning what's expected. The program just terminates. I think there needs a (l > h) terminate condition.

    • @senaholiz
      @senaholiz ปีที่แล้ว

      I noticed this too. This can easily be solved by another if chain in the first if.
      if () {
      ...
      }else if () {
      ...
      } else {
      return -1
      }

  • @garogarabed6196
    @garogarabed6196 5 ปีที่แล้ว

    The array starting at index 1 is confusing me, but in general awesome video! Thanks a lot!

  • @priyankagehlot9945
    @priyankagehlot9945 9 หลายเดือนก่อน +1

    Thankyou sir 🥰😄

  • @chaosavenger2
    @chaosavenger2 3 ปีที่แล้ว

    you'd also have to check if your lower bound is greater than you upper bound, such as for the input [2,5], target = 0

  • @rahulraj-hn1cd
    @rahulraj-hn1cd 6 ปีที่แล้ว +1

    Superb Sir ji thnq for giving algo video

  • @pranki2254
    @pranki2254 5 ปีที่แล้ว +1

    Sir, I just bought your Data Structures course on Udemy. I have working experience in Java but the course is taught in C and C++, will it help me in detail with respect to Java or will i be missing something?

  • @albinikitengeso9222
    @albinikitengeso9222 5 ปีที่แล้ว

    Kama umeletwa na ayuma gonga like twende sawa maana jamaa anataka tuelewe SI kumza

  • @mohammedadel8948
    @mohammedadel8948 2 ปีที่แล้ว

    Thank you for your hard work.

  • @jonelleyu1895
    @jonelleyu1895 2 ปีที่แล้ว

    So helpful! Thank you Sir! Btw would you please enable the subtitles? My english is not very good. Thank you.

  • @anupampaul2907
    @anupampaul2907 5 ปีที่แล้ว +2

    Sir, where is the condition to check (l>h)??

    • @adityac4
      @adityac4 5 ปีที่แล้ว

      how can be lower limit greater than the upper limit

    • @amberchawla7930
      @amberchawla7930 3 ปีที่แล้ว

      @@adityac4 it can come think of a subarray of 2 size and then mid is 0 index , now consider key smaller than mid

  • @SRNR_PODCAST.
    @SRNR_PODCAST. 3 ปีที่แล้ว +1

    a gold mine in youtube

  • @shardhajanani2962
    @shardhajanani2962 6 ปีที่แล้ว +4

    If they ask binary search in question paper which method have to follow either iterative method or recursive method ???

  • @rachit007
    @rachit007 4 ปีที่แล้ว +1

    Great explanation

  • @prateeksharma2839
    @prateeksharma2839 9 หลายเดือนก่อน

    Please sir ...i'm little bit confused... I need your help...please reply and tell me that binary search iterative method also use divide & conquer strategy or only recursive approach is using divide & conquer for binary search

  • @akshaysharma8235
    @akshaysharma8235 ปีที่แล้ว

    Sir, a function can return the function itself which you have done int binary search algorithm ???

  • @abhisheksinghrajput2624
    @abhisheksinghrajput2624 6 ปีที่แล้ว

    Thank you sir and great explanation

  • @rohanMJ
    @rohanMJ 5 ปีที่แล้ว +1

    shouldn't we have to pass array into the recursive function? or should array be a global variable. Am i wrong guys?

    • @intuitivej9327
      @intuitivej9327 3 ปีที่แล้ว

      Yes we need to pass array to the fuction.
      We can just capture the main logic behind so you will have no problem if it is kept in mind.

  • @supremoluminary
    @supremoluminary 4 ปีที่แล้ว

    What if there are duplicate values? We want the first one.
    Input:
    array= [1, 5, 5, 5, 6]
    value = 5
    Expect: 1.
    But your approach would divide the array in half and check the mid element, array[2]. array[2] is 5, and that matches the value you searching for, so your function would return 2. Instead, it should return 1.

  • @raakuu
    @raakuu 3 ปีที่แล้ว

    You are great❤️❤️

  • @adityakumarmishra5556
    @adityakumarmishra5556 5 ปีที่แล้ว

    Sir time we get by iterarative algorithm and recursive algo is same log(n) so which one is best for use .

  • @subhamroy5619
    @subhamroy5619 7 หลายเดือนก่อน

    a=[3,6,8,12,14,17,25,29,31,36,42,47,53,55,62]
    z=int(input("Enter the number you want to search: "))
    low=0
    high=len(a)-1
    while low

  • @pmahaur962
    @pmahaur962 8 หลายเดือนก่อน

    Sir what's the meaning of return in coding why we do this and when?????

  • @shobhitlamba3495
    @shobhitlamba3495 2 ปีที่แล้ว

    why aren't we using "while(h>=l)" in the else statement?

  • @ArshdeepSingh-nm1tt
    @ArshdeepSingh-nm1tt 5 ปีที่แล้ว +2

    i donno why he took that pause 0:02 , anyways he's best!

  • @gateeasycse
    @gateeasycse 5 ปีที่แล้ว

    Sir generly any problem written in iterative time complexity take more or equal to recursive ?

    • @gateeasycse
      @gateeasycse 5 ปีที่แล้ว

      Abdul Bari sir iterative and recursive are equailvent power? If yes then y we are using recursive ?plz explain .greedy, dynamic approach following iterative or recursive or both

  • @adipratapsinghaps
    @adipratapsinghaps 3 ปีที่แล้ว

    Sir, how is BinarySearch Divide and Conquer? We are not dividing the problem into multiple sub-problems. We are, at every step, decreasing the problem into a single smaller problem. We don't divide the array into 2 halves at each stage and process both arrays. I read on internet it is a Decrease and Conquer problem. Is this true?

  • @RameshMaity90
    @RameshMaity90 ปีที่แล้ว

    Batch of 2023 ❤ still masterpiece for Algorithm

  • @45_vrushalinikam19
    @45_vrushalinikam19 ปีที่แล้ว

    Thank you🙏

  • @sahilgurung8876
    @sahilgurung8876 4 ปีที่แล้ว +1

    Btw why is it t(n/2) for the returning functions even if rbinsearch function takes t(n) time they are the same aren't they

    • @pragmatic_p8
      @pragmatic_p8 3 ปีที่แล้ว +1

      it recursively called for only half of the array numbers...so n/2

    • @auroraz6911
      @auroraz6911 2 ปีที่แล้ว

      @@pragmatic_p8 Thanks for your clear explanation! I was also confused about it, but now I totally understand.

  • @SmokyBigSmoke
    @SmokyBigSmoke 4 ปีที่แล้ว

    Thank you so much sir.

  • @mohammedrizin3015
    @mohammedrizin3015 4 ปีที่แล้ว

    sir do we want to define the l and high values in recursive algorithm

  • @saivaraprasadmandala8558
    @saivaraprasadmandala8558 ปีที่แล้ว

    why isn't the order of binary search is O(log n to the base 2)

  • @laxmanreddysuram8812
    @laxmanreddysuram8812 2 หลายเดือนก่อน

    i didnt find videos on linked list.

  • @basavrajningadali4919
    @basavrajningadali4919 8 หลายเดือนก่อน

    sir please make videos on python also

  • @downtowngedi
    @downtowngedi 5 ปีที่แล้ว

    Sir, why did you put a condition to check whether l is less than h?

    • @downtowngedi
      @downtowngedi 5 ปีที่แล้ว

      What if the element isn't present in the array. The call should be returned rather than calculating mid in else.

  • @dark4o90
    @dark4o90 4 ปีที่แล้ว

    but this is not really D&C algorithm as it just reduces the problem but it does not generate separate results for sub-problems which are later merged on like in quicksort or parallel sum

  • @sahilchaudhariarsss
    @sahilchaudhariarsss 4 หลายเดือนก่อน

    mistake in algo-----> not checked condition for low>high if(low>high) return -1;

  • @divyanshutripathy3484
    @divyanshutripathy3484 4 ปีที่แล้ว

    Wouldn't the code still work if we only wrote what's in the main else block?

  • @seiftahawy54
    @seiftahawy54 4 ปีที่แล้ว

    Sir, you're great !

  • @kirandeepsingh9144
    @kirandeepsingh9144 6 ปีที่แล้ว +1

    where is combine step in binary search after divide and solve step?

    • @kirandeepsingh9144
      @kirandeepsingh9144 6 ปีที่แล้ว +1

      +Abdul Bari without combing it full fill the divide and conquer guidelines? as it says there are 3 phases divide conquer and combine

  • @jairam1741
    @jairam1741 4 ปีที่แล้ว

    If element not found you return zero but what will happen if index of the element is zero?

  • @karanssh
    @karanssh 3 ปีที่แล้ว

    4:46 you might want to mention that we return 0 after the last else block in case element not in array. Also, return 0 denotes success in some languages, better to return -1 or an error but that's just down to my personal preference

    • @adriann789
      @adriann789 3 ปีที่แล้ว

      No need to return in the last else block since returning the recursive function will eventually return l or -1 (not found) as per the first if (l == h) condition.
      By continually halving the array, we will eventually hit the base case where the array will only contain 1 element.

  • @krishnamohan1809
    @krishnamohan1809 5 ปีที่แล้ว

    Nice video sir,but i think it will not work for cases like key=1 and array=[2,3];

    • @KhwiloWatai012
      @KhwiloWatai012 4 ปีที่แล้ว

      The base case should be:
      if (low > high) {
      return -1;
      }
      Since 1 is less value than 2, the first element of the array, the recursion in this lesson will run without reaching its base case. At some point, high will be less than low which would then result in an error. So it's best to define your base case as the above example.

  • @superawesomeninja2
    @superawesomeninja2 3 ปีที่แล้ว

    This is a truly great video, but wouldn't we want to include the sorted array as one of the parameters for the recursive binary search?

  • @iamthere4u
    @iamthere4u 10 หลายเดือนก่อน +1

    Not satisfied 😢

  • @deepak1291
    @deepak1291 3 ปีที่แล้ว

    Algo worked fine when item is inside the array or greater then the array elements but fails, when item is less then the entire array elements.
    Pass:
    1-> When item is present
    2-> When item is greater then all the items present
    Failed: When Item is less then all the items
    int[] a = {2, 3, 4, 10, 40};
    int searchItem = 1;
    int low = 0;
    int arraySize = a.length;
    int high = arraySize - 1;
    private static int search(int[] a, int low, int high, int searchItem) {
    if (low == high) {
    if (a[low] == searchItem)
    return low;
    else
    return -1;
    }
    else {
    int mid = (low + high) / 2;
    if (searchItem == a[mid])
    return mid;
    if (searchItem < a[mid] )
    return search(a, low, mid-1, searchItem);
    else
    return search(a, mid+1, high, searchItem);
    }
    }
    Condition need to be added :
    Instead of: if (low == high) {
    we need to put one more check there:
    if (low >= high) {

  • @vigneshprabhu7626
    @vigneshprabhu7626 ปีที่แล้ว

    I love u sir you're my bahubali

  • @shishiradhikari1911
    @shishiradhikari1911 6 ปีที่แล้ว

    Don't we need to pass the Array as one of the parameters (which in this case is A)? It does not really matter but if you want to be pedantic then I think you should pass A as one of the parameters too.

    • @shishiradhikari1911
      @shishiradhikari1911 6 ปีที่แล้ว

      Makes sense.I was just being pedantic. Thanks for these awesome videos!

  • @sheraz_razzaq
    @sheraz_razzaq 5 ปีที่แล้ว

    Thank you.. Sir....

  • @RHIN0o
    @RHIN0o 5 ปีที่แล้ว

    thanks for this video!

  • @nachomacho7027
    @nachomacho7027 2 ปีที่แล้ว

    👍👍👍

  • @esgmodiyt1359
    @esgmodiyt1359 4 ปีที่แล้ว +1

    Besttttttttttt teacher

  • @aadarshmishra1488
    @aadarshmishra1488 5 ปีที่แล้ว

    mja aa gya sir

  • @TTAGGGTTAGGG
    @TTAGGGTTAGGG 9 หลายเดือนก่อน

    never though I'd have Prakash Raj teach me CS