Learn Binary Search in 10 minutes 🪓

Поделиться
HTML-код
  • Опубликовано: 18 окт 2024
  • 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...
    ===========================================================

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

  • @BroCodez
    @BroCodez  3 года назад +84

    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 3 года назад +1

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

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

      Feridusssssssssss

    • @MyQlus-iswatching
      @MyQlus-iswatching 9 месяцев назад

      thanks heres a comment cause you asked

  • @heayyyyyyy11
    @heayyyyyyy11 3 года назад +14

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

  • @luismatos684
    @luismatos684 Год назад +12

    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 Год назад +8

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

    • @passportbro904
      @passportbro904 2 месяца назад +1

      please stop calling random people god as our real god will ask you why u said that when u meet him. just saying, theres only 1, but bro is gods homie tho

  • @habboholoful
    @habboholoful 3 года назад +33

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

    • @mypie-5952
      @mypie-5952 11 месяцев назад

      @xavier from alternate universe be like

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

  • @nikaarudashvili1353
    @nikaarudashvili1353 3 года назад +21

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

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

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

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

  • @tiavanderyacht5702
    @tiavanderyacht5702 4 месяца назад

    You do an outstanding job of making the content easy to understand. You have helped me get through my class this quarter thank you!

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

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

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

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

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

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

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

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

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

    commenting for the algorithm
    love your stuff my guy 👍

  • @ethanalkout949
    @ethanalkout949 3 года назад +25

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

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

      You are pathetic.

    • @dazzledave
      @dazzledave 3 месяца назад +2

      How are u "done" lol

    • @Thats_Handle0
      @Thats_Handle0 3 месяца назад +1

      ​@@dazzledaveWell i guess he quiet or something

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

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

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

  • @aer0449
    @aer0449 3 года назад +6

    Thank you so much for making such a great content

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

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

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

  • @ganymedeshortride
    @ganymedeshortride 3 года назад +4

    Better than any movie.

  • @sankararaman900
    @sankararaman900 2 дня назад

    4:27 The index will not be -1 due to insertion point. So if the element not found the binarySearch method it will return (-(insertion point) - 1). The target element becomes the insertion point

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

    Bro you are genus !😍😍😍

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

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

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

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

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

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

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

    Amazing video bro, Thank You so much.

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

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

  • @lukrau15
    @lukrau15 22 дня назад

    you're the organic chem tutor of coding

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

    thank you. Your video helped a lot.

  • @ddelorez
    @ddelorez 11 месяцев назад

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

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

    Great video, thanks bro.

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

    You are the best!💻

  • @dr.caffeine232
    @dr.caffeine232 2 месяца назад

    You are smoother than Bro Code Liquor

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

    Thanks bro
    *ongoing comment crusade while i cram*

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

    Great explanation, thanks a lot!!

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

    Very well explained. Simple and understandable!

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

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

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

    Thank you, bro code! Excelent explaining.

  • @prodiptamondal1758
    @prodiptamondal1758 3 года назад +4

    Please also solve some leetcode problems

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

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

  • @eugene-white-shark
    @eugene-white-shark 3 года назад +2

    Nice tutorial

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

    good job bro....!

  • @Sub-zero1123
    @Sub-zero1123 3 года назад +1

    Thanks bro, this is awesome

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

    Dude is the GOAT fr

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

    Well done, thank you a lot bro.

  • @juanandresstingo
    @juanandresstingo 8 месяцев назад

    it would be nice a recursive explanation also.

  • @fauzanadityaharsya6146
    @fauzanadityaharsya6146 10 месяцев назад

    Thank you. Such a helpful video

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

    Nice and clear as always.

  • @투오아
    @투오아 6 месяцев назад

    shout out from South Korea

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

    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

  • @redscorpion7000
    @redscorpion7000 3 месяца назад

    what a legend ,watched his vid 5 days b4 exam and got a 97/100

  • @WissemMERRI
    @WissemMERRI 10 месяцев назад

    Great methodology 🎉 thx

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

    why not use low+high/2, in simple it works fine, but low and high values are large, then integers might overflow

  • @mindlessmeat4055
    @mindlessmeat4055 Год назад +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?

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

    So much knowledge

  • @Yeganeh_ht
    @Yeganeh_ht 8 месяцев назад

    this was helpful thank you

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

    Great vid

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

    Thank you.

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

    Thank you, sir 👍👍👍

  • @MrLoser-ks2xn
    @MrLoser-ks2xn Год назад +1

    Thanks!

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

    go super Bro!

  • @blackvampiremovie
    @blackvampiremovie 3 месяца назад

    thank you so much!

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

    Awesome!

  • @ashvikvijai8114
    @ashvikvijai8114 5 месяцев назад

    Thank you!

  • @CodeWithEssa
    @CodeWithEssa 5 месяцев назад

    Love it!

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

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

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

    Thanks, Bro!

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

    Wow nice channel bro❤

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

    BEST!

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

    Thanks

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

    good one bro!

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

    thanku sir

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

    bro code is bro god after this

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

    Nice class

  • @ntombizakhexego-jimlongo8577
    @ntombizakhexego-jimlongo8577 26 дней назад

    thank you

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

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

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

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

  • @ПауверТзен
    @ПауверТзен 11 месяцев назад

    thanks a lot)

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

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

    Ha nice 👍🏿

  • @ٱسمر
    @ٱسمر 3 года назад +1

    thx

  • @Talkatfive
    @Talkatfive 6 месяцев назад

    Nice

  • @Ri_sab55
    @Ri_sab55 5 месяцев назад

    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 ?

    • @Izzy-tl3gp
      @Izzy-tl3gp 19 дней назад

      i am wondering the same thing!

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

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

    cool

  • @aka-Monster01
    @aka-Monster01 8 месяцев назад

    u r awesome

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

    is this method usable to unsorted array?

  • @cashionsteven5695
    @cashionsteven5695 3 года назад +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 ?

  • @AbhijeetKumar-cm3jh
    @AbhijeetKumar-cm3jh 3 года назад +2

    Face reveal on 100k ?

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

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

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

    👍

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

  • @augischadiegils.5109
    @augischadiegils.5109 3 года назад +1

  • @Kerzyist
    @Kerzyist 11 месяцев назад +1

    Ehhhh Mazing

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

    package Java_Programs;
    import java.util.Arrays;
    import java.util.Scanner;
    public class BinarySearch
    {
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the size of the array: ");
    int size = input.nextInt();
    int[] arr = new int[size];
    System.out.print("Enter your target. Enter 0 or positive integers only: ");
    int target = input.nextInt();
    for (int i = 0 ; i < arr.length ; i++)
    arr[i] = i;
    int index = Arrays.binarySearch(arr, target);
    if (index == -1)
    System.out.println("
    Using the built-in binary search method in Java, the target " + target + " is not found.
    ");
    else
    System.out.println("
    Using the built-in binary search method in Java, the target " + target + " is found.
    ");
    int returnValue = binarySearchMethodTwo(arr, target);
    if (returnValue == -1)
    System.out.println("
    Using a user-created method, the target " + target + " is not found.");
    else
    System.out.println("
    Using a user-created method, the target " + returnValue + " is found.");
    }
    private static int binarySearchMethodTwo(int[] arr, int target)
    {
    int min = 0;
    int max = arr.length - 1;
    int steps = 1;
    System.out.println("User-created method:");
    while (min target)
    max = midPosition - 1;
    else
    return midPosition;
    }
    return -1;
    }

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

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

    sweet