Learn Binary Search in 10 minutes 🪓

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 พ.ค. 2021
  • binary search algorithm tutorial example explained
    #binary #search #algorithm
    // binary search = Search algorithm that finds the position
    // of a target value within a sorted array.
    // Half of the array is eliminated during each "step"
    music credits 🎼 :
    ===========================================================
    Up In My Jam (All Of A Sudden) by - Kubbi / kubbi
    Creative Commons - Attribution-ShareAlike 3.0 Unported- CC BY-SA 3.0
    Free Download / Stream: bit.ly/2JnDfCE
    Music promoted by Audio Library • Up In My Jam (All Of A...
    ===========================================================
  • วิทยาศาสตร์และเทคโนโลยี

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

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

    import java.util.Arrays;
    public class Main{

    // binary search = Search algorithm that finds the position
    // of a target value within a sorted array.
    // Half of the array is eliminated during each "step"

    public static void main(String[] args) {

    int array[] = new int[1000000];
    int target = 777777;

    for(int i = 0; i < array.length; i++) {
    array[i] = i;
    }

    //int index = Arrays.binarySearch(array, target);
    int index = binarySearch(array, target);

    if(index == -1) {
    System.out.println(target + " not found");
    }
    else {
    System.out.println("Element found at: " + index);
    }

    }
    private static int binarySearch(int[] array, int target) {

    int low = 0;
    int high = array.length - 1;

    while(low target) high = middle - 1;
    else return middle; //target found
    }

    return -1;
    }
    }

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

      Could you please make an in depth video on Binary Search Trees including how to balance them ?

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

      Feridusssssssssss

    • @MyQlus-iswatching
      @MyQlus-iswatching 4 หลายเดือนก่อน

      thanks heres a comment cause you asked

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

    I cannot believe how simple you made this. Every other video I saw eventually caused confusion. You clearly stated the concept first, where every thing that followed fell into place. This has been my experience in your other videos learning C#, and Python programming languages. God Bless You.

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

    U are a god. I have used your tutorials since I started programming in april 2022... You have no idea how much you have helped me out man!! all for free!!!!

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

    Had a task with binary search.
    Didn't understand -> open Bro Code Data Structures and Algorithms Playlist -> Found Binary Search -> Understood :D

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

    this was actually really nice. itd be nice if you had even more stuff on datastructures and algos so i can become an algo chad

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

    commenting for the algorithm
    love your stuff my guy 👍

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

    Hey, bro. I just wanted to let you know that when it comes to teaching and explaining code, you are the best of this world. I am not exaggerating; I have watched hundreds of channels, and taken tens of paid online courses, no one is as nearly as good as you.

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

    This example gave me a much better understanding of how searching works.

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

    Thank you so much for making such a great content

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

    You saved the day, AGAIN !
    Thanks so much bro, take care of you ! :)

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

    this channel should have more than a million subscribers!!!

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

    Thanks a lot friend! The first explanation is great! I am normally c# user but can easily follow your tuts since its similar. What i cant understand is how while loops is going to break, for example when start is greater end in that situation.

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

    A hi from "Brazil' Bro, code! You are the guy.

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

    Great explanation, thanks a lot!!

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

    Very well explained. Simple and understandable!

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

    You are underrated af

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

      For Sure

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

      @@niksonney4462 Yeah man I did ask my friends to subscribe him :)

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

    thank you. Your video helped a lot.

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

    Thank you so much Lord Bro Code. I was able to finish my Java final semester project thanks to you sir.

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

    well, you're an absolute legend
    that's all I got to say

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

    Your way of teaching is just fantastic bro🤩. You are the best. Love from India❤

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

    Amazing video bro, Thank You so much.

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

    Great video, thanks bro.

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

    Bro you are genus !😍😍😍

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

    Here I was over complicating it in my head thank you!

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

    You are the best!💻

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

    Jesus Christ, how such a great content is such an underrated... Horrifically underrated.
    P. S. : this English... I'VE BEEN WAITING FOR THE NON INDIAN CODE CONTENT FOR A YEARS...

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

    am officially done with "programming" but I will keep my subscription ❤

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

      You are pathetic.

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

    Thank you. Such a helpful video

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

    Nice and clear as always.

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

    I hope that you can do some tutorials about every data Structure or techniques... I love they way you teach

    • @mypie-5952
      @mypie-5952 6 หลายเดือนก่อน

      @xavier from alternate universe be like

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

    Well done, thank you a lot bro.

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

    Thank you, bro code! Excelent explaining.

  • @Sub-zero1123
    @Sub-zero1123 2 ปีที่แล้ว +1

    Thanks bro, this is awesome

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

    this was helpful thank you

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

    good job bro....!

  • @user-ts3of2mh6z
    @user-ts3of2mh6z 5 หลายเดือนก่อน

    Great methodology 🎉 thx

  • @eugene-white-shark
    @eugene-white-shark 3 ปีที่แล้ว +2

    Nice tutorial

  • @CodeWithEssa
    @CodeWithEssa 24 วันที่ผ่านมา

    Love it!

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

    Awesome!

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

    You Know One Thing!..... You Are My Bro.....

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

    Better than any movie.

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

    thank you!

  • @ashvikvijai8114
    @ashvikvijai8114 29 วันที่ผ่านมา

    Thank you!

  • @user-dq3he4xs5t
    @user-dq3he4xs5t 4 หลายเดือนก่อน

    Dude is the GOAT fr

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

    good one bro!

  • @pholoshomothiba3832
    @pholoshomothiba3832 11 หลายเดือนก่อน +1

    Great vid

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

    Thanks bro
    *ongoing comment crusade while i cram*

  • @mindlessmeat4055
    @mindlessmeat4055 11 หลายเดือนก่อน +1

    Love your video it is really great. How would you do a binary search if the array was filled with like 3 digit hex codes or some string similar to that? Like the first one would be AAA and the second would be AAB. Would I need to convert them to some number?

  • @JohnWick-hp8wn
    @JohnWick-hp8wn 3 ปีที่แล้ว +3

    So underrated....Bro keep goin❤...
    I know u hate it but waiting for ur OP android app development lol

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

    Thank you, sir 👍👍👍

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

    go super Bro!

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

    Thanks, Bro!

  • @MrLoser-ks2xn
    @MrLoser-ks2xn 11 หลายเดือนก่อน +1

    Thanks!

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

    Thank you.

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

    ... can you literally just teach me everything, math / science / life..... lol you vids are so clear and easy to understand

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

    So much knowledge

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

    Wow nice channel bro❤

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

    Underrated. Could you make one on Ternary Search Tree, Hashing (like double hashing), string search,MSD radix sort, RB trees.
    in your playlist. Would appreciate it

  • @user-cv1ek2ez3l
    @user-cv1ek2ez3l หลายเดือนก่อน

    shout out from South Korea

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

    Nice class

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

    This is essentially how you troubleshoot ground faults in a fire alarm notification circuit - split the circuit in the middle and look for the short to ground on either end (hopefully you don't find a short to ground on both ends).

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

    Thanks

  • @user-ge9gg4cp8d
    @user-ge9gg4cp8d 6 หลายเดือนก่อน

    thanks a lot)

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

    BEST!

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

    Nice

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

    good explanation but explain the while loop part too and why we are using middle +1 and middle -1 for effective understanding

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

    Hey bro, please make video of react js or angular 🤩

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

    Please also solve some leetcode problems

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

    thx

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

    it would be nice a recursive explanation also.

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

    it is a good video, nice, i like it papa

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

    thanku sir

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

    I love your videos, they are very helpful and easy to grasp. But I must admit my OCD gets triggered when you don't put your else on the same line as the closing curly brace of the respective if... xD

  • @aka-Monster01
    @aka-Monster01 3 หลายเดือนก่อน

    u r awesome

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

    cool

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

    We had to implement a binary search algorythm in the past semester in practical informatics. It wasn't really hard but I still ask myself if it is really used in the world or was it just a simple mind exercise to prep us for the whole algorythms and data structures shtick? I mean the code looks and works so simple, it just seems... I don't know, too simple.

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

      One example where I find it helpful is when searching through users in alphabetical order. There's no need to iterate through all the users to find one you're looking for

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

      Binary search is one of the easiest and most efficient searches for anything that is in ascending or descending order.

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

    bro code is bro god after this

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

    Ha nice 👍🏿

  • @user-oc5fn5xf1g
    @user-oc5fn5xf1g 6 วันที่ผ่านมา

    Hi bro, I still don't understand why we don't simply calculate middle like this : (high + low ) / 2 , since we are talking about indices than we won't face an overflow issue right ?

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

    👍

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

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

    why everyone finds middle with complex formula, instead of (low+high)/2, while both give same result???

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

    I like all your videos.
    can I ask why must int high =array.length-1 ? what is the need to -1 ? Hope you can explain how does it affect the result, thank you.

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

      can I try suggesting an explanation? Probably, that is because given an array {"A", "B", ... "J", "K"} as in the example at the beginning of the video, array.length = 11, however, since the first element index starts at 0, i.e. array[0] = "A", the last element index is array[10] = "K", or else array[array.length - 1] = "K". Thus, high = array.length - 1 = "K". Without this '-1' you will get The ArrayIndexOutOfBounds exception.

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

      the length of an array is always 1 greater than the last index of the array, because 0 is the first index and whatever is inside it is counted as the first element
      for example: 1, 2, 3, 4
      the 0th element is 1
      the 1st element is 2
      the 2nd element is 3
      the 3rd element is 4
      last element = 3rd element = 3
      array length is 4 (1 to 4)

  • @Kerzyist
    @Kerzyist 6 หลายเดือนก่อน +1

    Ehhhh Mazing

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

    Hey. Does this actually work for the highest value? If target is at position arr.length -1 will it find it? Because i tried implementing it and for that particular case it didn't work (returned -1. I probrably have some mistake though but thought of asking too).

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

      It should work, low should get larger until it equals high (arr.length-1). Once it reaches that, if arr[arr.length-1] ==target, it should return true. Otherwise high will be lower than low and the function returns -1

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

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

    Great, but this only works for numbers. In a situation where one would need to search an array of strings, you'd have to compare the middle value to the search query/target like so:
    x = query.compareTo(middleValue);
    if (x > 0) {
    minIndex = middleIndex + 1;
    } else if (x < 0) {
    maxIndex = middleIndex - 1;
    }
    else {
    return middleIndex;
    }

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

    fellow bro

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

    Hi there, recently i change the int low = 1 end the step to find the target is lower than int low = 0:
    Any explaination pls ?

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

    gucci gang gucci gang gucci gang gucci gang gucci gang gucci gang

  • @AbhijeetKumar-cm3jh
    @AbhijeetKumar-cm3jh 3 ปีที่แล้ว +2

    Face reveal on 100k ?

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

    6:13 Why not (high+low)/2 so average of two elements is the middle? Im not saying your is wrong, just it feels weird.

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

      I suppose that would work too!
      The book I read used: low + (high - low)/2
      I thought there might be something I might be missing if I changed formulas

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

      @@BroCodez No it won't work and the value of middle will repeat itself again and again...

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

      It will stuck in a constant loop

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

      Because (high + low) / 2 has a possibility of exceeding the Integer Range. i.e: (int + int)/2 // this could exceed Integer range hence breaking your code.
      (int) + (int- int)/2 The possibility of exceeding Integer range is minimum.

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

      yeah middle = (low+high)/2; is work, replace the formula that bro use. You see the same result!!! happy coding💲💲

  • @n.unknown.guy.6661
    @n.unknown.guy.6661 11 หลายเดือนก่อน +1

    is this method usable to unsorted array?

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

    why do we need the int middle and int value inside the while loop? why doesnt it work when we have them outside as global variables?

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

    $ey bro

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

    Do you think you can help me with my javafx project? I can pay you please! I need to implement a searchbar on a tableview I got from input

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

      I would love to help, but I barely have any time with running this channel and my day job :(

  • @GamingPro-xn4hy
    @GamingPro-xn4hy ปีที่แล้ว +1

    Help the yt algorithm

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

    Feriduussssssss

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

    yo, random comment

  • @AayushkumarSingh-nd1hm
    @AayushkumarSingh-nd1hm 4 หลายเดือนก่อน

    Y