2.6.1 Binary Search Iterative Method

Поделиться
HTML-код
  • Опубликовано: 29 сен 2024
  • Divide and Conquer Method
    Binary Search Method
    Iterative Algorithm
    Analysis of Binary Search Algorithm
    PATREON : www.patreon.co...
    Courses on Udemy
    ================
    Java Programming
    www.udemy.com/...
    Data Structures using C and C++
    www.udemy.com/...
    C++ Programming
    www.udemy.com/...

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

  • @joelperezurcelay
    @joelperezurcelay 5 лет назад +396

    the audio gets better after 5 minutes or so. Thank you for your videos!!

  • @ivybrandyn
    @ivybrandyn 5 лет назад +545

    Holy shit. I am a senior student of Computer Science and I never REALLY understood these concepts until I watched these videos. I am literally watching the entire playlist to understand Analysis of Algorithms better. You are amazing for taking the time to make these videos. Sending thanks all the way from Texas!

    • @lifeexplorer2965
      @lifeexplorer2965 5 лет назад +10

      Same here

    • @raadal-husban654
      @raadal-husban654 4 года назад +7

      @@lifeexplorer2965 Same here! (Master's student!)

    • @harish-wi3ts
      @harish-wi3ts 3 года назад +9

      I am a ECE student i learned all the concepts by my own...i am just here for refresh the concepts...only.

    • @yoonjiwoo7487
      @yoonjiwoo7487 3 года назад +8

      fck textbooks they only made me lost time

    • @exe.m1dn1ght
      @exe.m1dn1ght Год назад +12

      you are a senior cs student and you can't understand binary search ? wow, what do they teach you in that school ?

  • @dhruvseth
    @dhruvseth 4 года назад +8

    Great video Mr. Bari sir continue to educate CS students as myself, you have done a great job with this series and everyday I wake up knowing that I will learn something new from the master of teaching himself.

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

    A gem , I was tired of watching multiple videos but understood from Bari sir . Thanks a lot sir

  • @super_sao8339
    @super_sao8339 3 года назад +3

    I always thought that I'd been kicked in the head by a donkey until I saw this video. You save my life, thank you very much

  • @siamaralte1864
    @siamaralte1864 3 года назад +2

    Thanks alot Sir, your explanations always meet my expectation.
    I'm amazed on how you make things look so easy. 👏👏

  • @souravdas7263
    @souravdas7263 5 лет назад +3

    Sir u r great..plz makes videos on data structures with code and give some problems to practice.

  • @chetanupadhyay8409
    @chetanupadhyay8409 7 месяцев назад +1

    Best explanation forever

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

    The best teacher in the world!

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

    One of the best explanations ever

  • @syedchand995
    @syedchand995 5 лет назад +6

    Missing you sir 🙂

  • @EjazAhmed-pf5tz
    @EjazAhmed-pf5tz 2 года назад

    thank you so much they way you understand algo
    it was outstanding

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

    Case - Where an element is not present in the tree - instead of quitting when high is less than low, can we use when high==low check that element else return false.

  • @vibhor0202
    @vibhor0202 5 лет назад

    In your code, you return 0 if index is not found, but instead you should return False; returning 0 index will mean that element is found as the first item on array
    Here is python code for the same that you explained :
    def binarySearch(A,key):
    n = len(A)
    low = 0
    high = n
    while (low

  • @deepakborah8335
    @deepakborah8335 5 лет назад +1

    Really amaging explainations

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

    Very well explained, thank you so much

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

    Thank you sir for this video

  • @manojg4451
    @manojg4451 5 лет назад

    Sir, what would be algorithm if the requirement is if target element is not found, return the closest element. Do we have compare the absolute difference of lowest-target , middle-target& highest-target once the while condition is exited

  • @UsmanGhani-wk6hq
    @UsmanGhani-wk6hq 4 года назад +1

    Why we didn't take the list from "0" index i.e. 0-14 in this case? Thanks.

    • @apostolosmavropoulos177
      @apostolosmavropoulos177 4 года назад +2

      cause its pseudocode..it doesnt follow faithfully the synatx of C langugae..when u implement it in C language u should declare the function as
      int binary_search(int A[ ], int key, int n) //as u know the arrays start form 0 till n-1 if we have declared int A[n];
      {
      low=0;
      high=n-1;
      etc...
      }

  • @SusheelKumar-dq7np
    @SusheelKumar-dq7np 5 лет назад

    Sir small dought the size of array is 12 so low value is 1 and high value is 12 mid= 1+12/2 in this case what is the mid value can you please conform sir

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

    Really good 👍 thanks so match

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

    Jazakallah Khair

  • @rohitgadkari8034
    @rohitgadkari8034 6 лет назад

    Sir it's really help for me

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

    Guys plzz write mid inside the while loop

  • @soudiptadutta6886
    @soudiptadutta6886 6 лет назад

    Please take few problems on Binary Search and solve it. That is the best way to make others understand Binary Search and it's implementations.
    Here is the complete code to find an element in the array:
    public class BinarySearch_Iterative {
    public static void main(String[] args) {
    int[] a = {1, 2, 3, 5, 42, 43, 56};
    Arrays.sort(a);
    System.out.println(Arrays.toString(a));
    System.out.println(fix(a,5));
    }//amin
    private static int fix(int[] a, int p) {
    int start = 0 ; int end = a.length -1;
    if(a[start] == p) return a[start];
    while(end >= start + 1) {
    int mid = start + (end -start)/2 ;
    if(a[mid] == p ) return mid ;
    if(p > a[mid]) {start = mid + 1 ;}//if
    if(p < a[mid]) { end = mid -1 ;}
    }
    return -1;
    }//fix
    }//end1

  • @PankajYadav-kb2pu
    @PankajYadav-kb2pu 2 года назад

    If the mid value is in fraction , what to do ?

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

    Sir please clear my confusion.
    If an array contains multiple occurrences of a given element, will a binary search output the position of the first occurrence or the last occurrence or all occurrence?

    • @abdul_bari
      @abdul_bari  4 года назад +1

      Search is performed on distinct set of elements only.

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

      @@abdul_bari thank u sir

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

    but the arrays start from 0. we will the first element if we start from 1.

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

    Sir Ji thank you

  • @indumathi5182
    @indumathi5182 5 лет назад

    Sir can u explain plain applications of Euler 'S function on cryptography

  • @sahithchary4392
    @sahithchary4392 2 года назад +1

    God ⚡⚡⚡⚡

  • @IoannisAnifantakis
    @IoannisAnifantakis 4 года назад +233

    Abdul Bari is of the most gifted instructors I have ever seen on online videos. Thank you very much for the level of detail and the clarity of your explanations. I can say that your videos are the solid foundation for literally anyone studying these subjects before watching anything else. Congratulations!

  • @sratnamanjari244
    @sratnamanjari244 5 лет назад

    Thank You so so much Sir 🙏😊

  • @sandeepmandrawadkar9133
    @sandeepmandrawadkar9133 5 лет назад +88

    Algorithms has become "Bachchon ka Khel" after watching your videos! Kudos, you're ultimate!!!

  • @arjunchauhan6108
    @arjunchauhan6108 5 лет назад +75

    GOD DAMN. I have been through Udacity, Khan Academy, Coursera, MIT etc courses and THIS is by far the best explanation I have encountered. Holy sheeeeeet. Unbelievable.

  • @fanalgo
    @fanalgo 4 года назад +49

    Note that in practice doing mid = (L+H)/2 is not a good idea because it can cause an overflow since (L+H) can be greater than the list size.
    Instead, we should do mid = L+(H-L)/2 to avoid any possible overflow.

    • @thean1585
      @thean1585 2 года назад +4

      Damn I was stuck on this in python for a while, took your advice and it works, you sir are a legend!

    • @rishav179
      @rishav179 Год назад +2

      we can do the floor division to avoid overflow.

    • @davidomar742
      @davidomar742 Год назад +7

      if you start l at 0 and h at length of list -1 you won't overflow

  • @Risefromtheashes689
    @Risefromtheashes689 5 лет назад +90

    Proudly he is from india❤💕🇮🇳

    • @hennaartbyreeka2675
      @hennaartbyreeka2675 3 года назад +5

      Proudly he is a MUSLIM.

    • @Risefromtheashes689
      @Risefromtheashes689 3 года назад +7

      @@hennaartbyreeka2675 wtf

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

      @@Risefromtheashes689 yeah

    • @piushpaul4880
      @piushpaul4880 3 года назад +7

      @@hennaartbyreeka2675 allah didn't help him to gain knowledge you fool...it was his efforts 😂😂

    • @piushpaul4880
      @piushpaul4880 3 года назад +8

      @@hennaartbyreeka2675 if that is so ...he would have done namaz 😂😂😂😂 and not learn algorithms

  • @prachinainawa3055
    @prachinainawa3055 4 года назад +41

    So amazing. India needs more teachers like you.
    You explain everything about the topic that's what I love the most.

  • @vishallondhe7298
    @vishallondhe7298 4 года назад +15

    lol .. removed headphone and changed settings 4 times thinking the problem was on my end.. btw thanks for your lectures, you are a great teacher.

  • @abhishekmahapatra4270
    @abhishekmahapatra4270 5 лет назад +13

    Sir,Array's counting starts from 0 to (n-1) but you have started the counting from 1?Is it possible to do that?

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

      also when the key is not found the return should be -1

    • @mubashir1996
      @mubashir1996 4 года назад +2

      usually in sudo code, counting starts from 1 to n.

  • @deepikatanmayi801
    @deepikatanmayi801 3 года назад +16

    I regret paying thousands of rupees to my college faculty. Abdul sir's explanation is always on point!

  • @alimusic2691
    @alimusic2691 2 года назад +2

    I understand 15 is the amount of nodes, and log2 n. Why 15+1? why +1?

  • @LikiLiki-xk9im
    @LikiLiki-xk9im Год назад +1

    What if array contains even elements? Suppose 16 total
    Low=1 and high=16
    Mid=(1+16)/2 =8.5
    Should we take mid has int so that it becomes int mid=8?
    Is that correct?

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

      Same question 🤔................... sir please answer

  • @CoderIRQ
    @CoderIRQ 2 года назад +6

    Most gifted instructor I have ever seen on online videos like this. Thank you very much your explanations.

  • @krishsandy8140
    @krishsandy8140 6 лет назад +40

    sir,please make videos on data structures also.

    • @geekyprogrammer4831
      @geekyprogrammer4831 5 лет назад +2

      you can buy it from Udemy!

    • @vaibhav.polska
      @vaibhav.polska 4 года назад +1

      Yeah Purchase the course on Udemy. It is great!!

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

      @@geekyprogrammer4831 is the course instructor himself?

    • @harshmk1052
      @harshmk1052 4 года назад +1

      @@gavravdhongadi9824 yep .
      I had also bought that course .
      It is worth every penny .

  • @JiaxunLi-no4hd
    @JiaxunLi-no4hd 28 дней назад +1

    Thank you so much!! You make everything so clear. I will continue to learn with you!!

  • @subramaniyanvg6367
    @subramaniyanvg6367 3 года назад +5

    If someone asks me to write binary search in future I will understand and I will write it instead of byhearting. You explained in such a way people will understand to the core. Thank you sir.

  • @batman_1st
    @batman_1st 6 лет назад +34

    what's with the sound?

  • @pratiknaik8503
    @pratiknaik8503 3 года назад +1

    indexing in the array stats from 0 right ?

  • @CodePinaka
    @CodePinaka 3 года назад +3

    ignoring my university lecture (bcz they just read ) bcz i know Abdul Sir would make me understand in a better way !!!!!!!.
    Thanks a lot sir. You are just awesome.

  • @mehrajahmadbhattt
    @mehrajahmadbhattt 4 года назад +4

    Now Algorithm is My Favourite Subject

  • @fivecube
    @fivecube 5 лет назад +2

    Sir at 16:14 you are calculating wrong height. The height of that tree is 3 not 4.(No of edges are counted and not the nodes).
    The number of comparison depends upon height of a Complete binary tree as follows:- floor(logn) + 1
    Anyways, Your content is awesome.

    • @piyushtayal8567
      @piyushtayal8567 5 лет назад

      Ya sir right! By the way your channel is great too!!

  • @sultangaddafi8698
    @sultangaddafi8698 5 месяцев назад +2

    Best Binary Search Algorithm Video Ever!!!!!

  • @mohammadjamiluddin3880
    @mohammadjamiluddin3880 5 лет назад +6

    Superb!!! Sir, you are great.... Fantastic... You made this course a piece of cake.

    • @Farahat1234
      @Farahat1234 5 лет назад

      are you studying analysis and design of algorithm?

  • @ivishalsr2021
    @ivishalsr2021 Год назад +1

    sir i have only watched this video of yours and i am already your fan! this was very very very much helpful! sir charan kahan hai aapke ashirvaad de do plz

  • @nehajaiswal4748
    @nehajaiswal4748 3 года назад +2

    This is really great video thanks a lot

  • @TTAGGGTTAGGG
    @TTAGGGTTAGGG 5 месяцев назад +1

    play the video at a faster speed to mitigate the audio issues

  • @nemesizzz8622
    @nemesizzz8622 2 года назад +1

    your surname in armenian means kind , thank you for your videos

  • @arjumandvillagelife
    @arjumandvillagelife 3 года назад +1

    you help me alot ,
    thanks for sharing this concept,
    now i got how binary search works,
    really nice work, wonderful presentation

  • @sadiaabbas2515
    @sadiaabbas2515 2 года назад +3

    Sir you’re best teacher ever
    Ma Shaa Allah

  • @mohamedagagna2120
    @mohamedagagna2120 Год назад +1

    May Allah reward you for this explanation
    May God bless you and make it in the balance of your good deeds

  • @sathyamanikanta2784
    @sathyamanikanta2784 8 месяцев назад +1

    Best 19 minutes ever❤❤❤❤

  • @eceaknc7523
    @eceaknc7523 5 лет назад +3

    Sir, generally i understand your videos, i understand the concept of algorithms but i want to make practice for understand better, what would you suggest for practice? Thank you

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

      geeksforgeeks.org

    • @vaibhav.polska
      @vaibhav.polska 4 года назад

      I think you should firstly Master the Data Structures and practice these algorithms on any competitive Programming site. I personally use Hackckerrank and Codechef.

  • @sirwr7790
    @sirwr7790 Год назад +1

    Assalamualaikum thanks a lot I understand it

  • @junkeekim2853
    @junkeekim2853 3 года назад +2

    So good! Much more understandable than the video of self-claimed "competitive programmers" out there!

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

    Sir Aapko Chumma Dene ka Karta h ek video m hi kai baar. M apni aukaat badhane k liye acchi companies ki preparation kar rha hu. Now its seems possible.

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

    For your information Abdul sir also have DSA course in Udemy & the price is just 350-500 INR ($4).
    JUST GO and buy that course he will make your career

  • @sauravkumar7508
    @sauravkumar7508 5 лет назад +3

    Can we write else if(key

  • @shamshtabrez6492
    @shamshtabrez6492 6 лет назад +2

    Nice tutorials.While making videos, plz give basic details of the topic as well as its implementation in java.I apprerciate your effort to share knowledge which will have global reach.

  • @abdallaelmedani8933
    @abdallaelmedani8933 3 года назад +3

    Thank you, Abdul. Quality materials!

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

    Agar mjhe 30 Lakhs k upper ka Package mila tou m aapko duniya m kahi bhi dhood nikalunga ar aapse milne aaoonga. Promise

  • @МаксимБазанов-и9э
    @МаксимБазанов-и9э 3 года назад +2

    Your explanations are brilliant! Greetings from Russia

  • @chinmaydas4053
    @chinmaydas4053 6 лет назад +1

    Sir you are great..helpful materials but it will be more useful if you explain topics from more basic concepts and would be best if you discuss these concepts in java or c# simultaneously..huge respect and love for you sir ❤️🙏🙏...

  • @compilerrun5516
    @compilerrun5516 4 года назад +1

    Sir can we take l=0 and h=14 ?

  • @negativenagesh
    @negativenagesh Год назад +1

    Bari ji Keerthan holla aapka bahut bada fan hai, aur aapka ghanta bajata hai hamara hostel mei.. thank u

  • @abcde2161
    @abcde2161 6 лет назад +1

    In the algorithm, low=1
    Where index of the array starts from 0..how is this working??
    Plz help

  • @SAGAR-vv3pc
    @SAGAR-vv3pc Год назад +1

    He is Bramha Of algorithms.

  • @isaacdanso7323
    @isaacdanso7323 3 года назад +7

    So far the best video on this topic I have ever watched he makes it looks simple and easy to comprehend. Thumbs up Sir

  • @uttkarshrastogi1764
    @uttkarshrastogi1764 4 года назад +2

    great work !! can't thank you enough it really boosted my confidence

  • @lateralot2179
    @lateralot2179 3 года назад +2

    the simplicity and thoroughness of your explanations are beautiful. thank you sir.

  • @EdwardMaingehama
    @EdwardMaingehama 11 дней назад

    I love this
    God raised such an Amazing Teacher!
    Glory To God

  • @nightking3013
    @nightking3013 5 лет назад +2

    Sir thanks a lot for making such awesome videos .Your videos are too good! You teach far better than an average teacher.

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

    n=12000 h toh binary search algorithm ka maximum running time kya hoga answar plz

  • @amiteshsahay9207
    @amiteshsahay9207 6 лет назад +1

    Abdul Sir, as we know that the list/array starts with 0, so ideally in our case the list shud had started with 0 and ended with 14, isn't that correct? In that case the result would come within 2nd step.....May be my understanding is wrong, plz correct me.

  • @amitranjan6998
    @amitranjan6998 6 лет назад +1

    Sir, i have confusion at level 0 there is 1 node ,at 1st level there is 2^1 node , at 2nd level there is 2^2 node and at 3rd level there is 2^3 node so at kth level there is 2^k node ... so finding time complexity we do 1+2^1+2^2+2^3.....2^k ,it form of GP , so we can write 2^(k+1)-1 ; assuming n-k=0 ; n=k we can write 2^(n+1)-1=2^n 2^2 +1 ,so time complexity will be o(2^n)....why it can't be o(2^n) ?

    • @Thepankaz1
      @Thepankaz1 5 лет назад

      because we are not going to all the nodes, we take only 1 path, what is displayed is all possibilities while searching, but if we search a element we explore only 1 path to the leaf in max worst case scenario.

  • @Mark-vv8by
    @Mark-vv8by 3 года назад

    I've seen some kind of problems that the solution is to do a binary search on the answer, can someone pls explain me that
    I can't find any video about it.

  • @yashikabajaj6573
    @yashikabajaj6573 3 года назад +1

    Sir, U explained this algorithm with so ease........ Thanku so much for ur guidance.....Looking forward for more such tutorials.

  • @KrishanuBanerjee-rv3wy
    @KrishanuBanerjee-rv3wy 4 месяца назад

    all i understand but i can't understand how can we consider the low index as 1.... it should be zero.....

  • @AliRaza-kq6sm
    @AliRaza-kq6sm 4 года назад +1

    can we check low and high for key before dividing array for mid (then we check it on and and so on ) me be the number in the location of low or high

  • @ambreenkawish2145
    @ambreenkawish2145 4 года назад +1

    Well done sir from Pakistan 🇵🇰👍

  • @arbajmomin
    @arbajmomin 5 лет назад +1

    Sir, Sachmuch maine aap jaisa explain krne wale sir nahi dekhe,
    You will a good motivation towards understanding programming in a good manner ..
    And plz upload concepts regarding c++ and data structures..
    Again thank you for such great explanation....🤗👍🏻

  • @aliasadraza5325
    @aliasadraza5325 4 года назад +1

    Sir I have watched all of your lecture,kindly sir insertion sort per koi video zaror bnay

  • @cricdubb4u963
    @cricdubb4u963 3 года назад +1

    The audio gets better after 5:25

  • @priyasingh0113
    @priyasingh0113 4 года назад +1

    echo is been produced the entire vedio sir, n gud luck for the productive lectures u gave, kindly upload some more fruitful vedios on computer architecture or other subjects

  • @120183PAV
    @120183PAV 4 года назад +2

    Thank you. These videos are great tutorials.

  • @vkmrishad
    @vkmrishad 3 года назад +1

    Nice, Good Explanation

  • @pr4_kp
    @pr4_kp 4 года назад +1

    thanks bro there were so many recurrence relations tho but i guess if you're actually gonna do cs thats pretty good

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

    Sir what if the mid value comes in decimal form...say like L=1 and H=4 then mid (1+4)/2 ...then it will be 2.5 , what should i do in this situation.. kindly reply

    • @RameshMaity90
      @RameshMaity90 9 месяцев назад

      Rule :- Always takes integer value.
      . That is if 2.5 takes 2 ... ### neglect decimal part .. Hope it is clear

  • @shreyasakshisingh
    @shreyasakshisingh 5 лет назад +1

    Sir ,if we are supposed to do range searching i.e. for finding all the values in a sorted array , whose values fall between L and U (inclusive),where L

    • @dhruvmalhotra4736
      @dhruvmalhotra4736 5 лет назад

      Easy!! find indexes of lower_bound == L and upper_bound== U from the array. All the elements can be found if we know lower and upper bound. Lower bound and upper bounds can be found using binary search. So complexity will be O(logn).

  • @pritomdasradheshyam2154
    @pritomdasradheshyam2154 4 года назад +2

    The explanations were so precise and clear cut. Thankyou so much Sir :)