Learn Binary Search in 10 minutes 🪓

  Рет қаралды 124,494

Bro Code

Bro Code

Күн бұрын

Пікірлер
@BroCodez
@BroCodez 3 жыл бұрын
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 жыл бұрын
Could you please make an in depth video on Binary Search Trees including how to balance them ?
@xaliqesgerzade6526
@xaliqesgerzade6526 Жыл бұрын
Feridusssssssssss
@MyQlus-iswatching
@MyQlus-iswatching 10 ай бұрын
thanks heres a comment cause you asked
@heayyyyyyy11
@heayyyyyyy11 3 жыл бұрын
Had a task with binary search. Didn't understand -> open Bro Code Data Structures and Algorithms Playlist -> Found Binary Search -> Understood :D
@luismatos684
@luismatos684 Жыл бұрын
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 2 жыл бұрын
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 3 ай бұрын
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
@paultvshow
@paultvshow 8 ай бұрын
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.
@habboholoful
@habboholoful 3 жыл бұрын
I hope that you can do some tutorials about every data Structure or techniques... I love they way you teach
@mypie-5952
@mypie-5952 Жыл бұрын
@xavier from alternate universe be like
@prizepig
@prizepig 2 жыл бұрын
This example gave me a much better understanding of how searching works.
@theuberman7170
@theuberman7170 3 жыл бұрын
Thank you so much Lord Bro Code. I was able to finish my Java final semester project thanks to you sir.
@sankararaman900
@sankararaman900 Ай бұрын
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
@tiavanderyacht5702
@tiavanderyacht5702 5 ай бұрын
You do an outstanding job of making the content easy to understand. You have helped me get through my class this quarter thank you!
@monemperor1559
@monemperor1559 3 жыл бұрын
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
@sanjana9599
@sanjana9599 3 жыл бұрын
this channel should have more than a million subscribers!!!
@ddelorez
@ddelorez Жыл бұрын
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).
@mykyta1235
@mykyta1235 Жыл бұрын
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...
@freakintruder
@freakintruder 2 жыл бұрын
Your way of teaching is just fantastic bro🤩. You are the best. Love from India❤
@dcentdevelopers3435
@dcentdevelopers3435 3 жыл бұрын
You Know One Thing!..... You Are My Bro.....
@jordanmartin2632
@jordanmartin2632 Жыл бұрын
Here I was over complicating it in my head thank you!
@TheKr0ckeR
@TheKr0ckeR Жыл бұрын
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 Жыл бұрын
well, you're an absolute legend that's all I got to say
@nikaarudashvili1353
@nikaarudashvili1353 3 жыл бұрын
You are underrated af
@niksonney4462
@niksonney4462 3 жыл бұрын
For Sure
@aer0449
@aer0449 3 жыл бұрын
@@niksonney4462 Yeah man I did ask my friends to subscribe him :)
@ethanalkout949
@ethanalkout949 3 жыл бұрын
am officially done with "programming" but I will keep my subscription ❤
@mykyta1235
@mykyta1235 Жыл бұрын
You are pathetic.
@dazzledave
@dazzledave 4 ай бұрын
How are u "done" lol
@Thats_Handle0
@Thats_Handle0 4 ай бұрын
​@@dazzledaveWell i guess he quiet or something
@JoyKamwendo
@JoyKamwendo Ай бұрын
Y
@ganymedeshortride
@ganymedeshortride 3 жыл бұрын
Better than any movie.
@carlosramon4981
@carlosramon4981 2 жыл бұрын
A hi from "Brazil' Bro, code! You are the guy.
@worldwide6626
@worldwide6626 3 жыл бұрын
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
@worldwide6626
@worldwide6626 3 жыл бұрын
and maybe heaps
@lukrau15
@lukrau15 Ай бұрын
you're the organic chem tutor of coding
@keerthivasanmohan1974
@keerthivasanmohan1974 8 ай бұрын
good explanation but explain the while loop part too and why we are using middle +1 and middle -1 for effective understanding
@sriharivernekar2255
@sriharivernekar2255 Жыл бұрын
Amazing video bro, Thank You so much.
@dr.caffeine232
@dr.caffeine232 3 ай бұрын
You are smoother than Bro Code Liquor
@AnaGuerra03
@AnaGuerra03 Жыл бұрын
thank you. Your video helped a lot.
@sergeistankovski7250
@sergeistankovski7250 3 жыл бұрын
You are the best!💻
@germanhoyos4422
@germanhoyos4422 2 жыл бұрын
... can you literally just teach me everything, math / science / life..... lol you vids are so clear and easy to understand
@RinInABin
@RinInABin 3 жыл бұрын
commenting for the algorithm love your stuff my guy 👍
@RamjeUthayakumaar
@RamjeUthayakumaar 10 ай бұрын
Dude is the GOAT fr
@Kwame_Kwao
@Kwame_Kwao 3 жыл бұрын
Bro you are genus !😍😍😍
@jevardspproxyacct6832
@jevardspproxyacct6832 2 жыл бұрын
Thanks bro *ongoing comment crusade while i cram*
@aer0449
@aer0449 3 жыл бұрын
Thank you so much for making such a great content
@eugene-white-shark
@eugene-white-shark 3 жыл бұрын
Nice tutorial
@redscorpion7000
@redscorpion7000 4 ай бұрын
what a legend ,watched his vid 5 days b4 exam and got a 97/100
@DineshKumar71
@DineshKumar71 2 ай бұрын
why not use low+high/2, in simple it works fine, but low and high values are large, then integers might overflow
@stoiandelev12345678
@stoiandelev12345678 Жыл бұрын
Very well explained. Simple and understandable!
@투오아
@투오아 7 ай бұрын
shout out from South Korea
@prodiptamondal1758
@prodiptamondal1758 3 жыл бұрын
Please also solve some leetcode problems
@wolanus
@wolanus 3 жыл бұрын
Great video, thanks bro.
@JohnWickXD
@JohnWickXD 3 жыл бұрын
So underrated....Bro keep goin❤... I know u hate it but waiting for ur OP android app development lol
@Remolhunter97
@Remolhunter97 3 жыл бұрын
You saved the day, AGAIN ! Thanks so much bro, take care of you ! :)
@vklmao8677
@vklmao8677 3 жыл бұрын
good job bro....!
@juanandresstingo
@juanandresstingo 9 ай бұрын
it would be nice a recursive explanation also.
@rafl277
@rafl277 Жыл бұрын
Great explanation, thanks a lot!!
@pholoshomothiba3832
@pholoshomothiba3832 Жыл бұрын
Great vid
@josiahmartin6463
@josiahmartin6463 3 жыл бұрын
So much knowledge
@sanjays9082
@sanjays9082 3 жыл бұрын
Hey bro, please make video of react js or angular 🤩
@maxhill9037
@maxhill9037 8 ай бұрын
why everyone finds middle with complex formula, instead of (low+high)/2, while both give same result???
@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; }
@ПауверТзен
@ПауверТзен Жыл бұрын
What about unicode?
@mohammad_zrar
@mohammad_zrar 2 жыл бұрын
Well done, thank you a lot bro.
@baubaudinamo
@baubaudinamo 3 жыл бұрын
go super Bro!
@chadsmith71
@chadsmith71 10 ай бұрын
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
@dandantin
@dandantin 2 жыл бұрын
Thank you, bro code! Excelent explaining.
@Sub-zero1123
@Sub-zero1123 3 жыл бұрын
Thanks bro, this is awesome
@dreamphoenix
@dreamphoenix Жыл бұрын
Thank you.
@fauzanadityaharsya6146
@fauzanadityaharsya6146 11 ай бұрын
Thank you. Such a helpful video
@projectspace786
@projectspace786 Жыл бұрын
Awesome!
@WissemMERRI
@WissemMERRI 11 ай бұрын
Great methodology 🎉 thx
@hakikatsingh6254
@hakikatsingh6254 Жыл бұрын
bro code is bro god after this
@oswallt06
@oswallt06 Жыл бұрын
Nice and clear as always.
@hannibalbianchi1466
@hannibalbianchi1466 3 жыл бұрын
Thank you, sir 👍👍👍
@mindlessmeat4055
@mindlessmeat4055 Жыл бұрын
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?
@strex2062
@strex2062 3 жыл бұрын
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 жыл бұрын
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 жыл бұрын
@@BroCodez No it won't work and the value of middle will repeat itself again and again...
@bsagar5306
@bsagar5306 3 жыл бұрын
It will stuck in a constant loop
@sarabjeetsinghhora7751
@sarabjeetsinghhora7751 2 жыл бұрын
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 жыл бұрын
yeah middle = (low+high)/2; is work, replace the formula that bro use. You see the same result!!! happy coding💲💲
@Yeganeh_ht
@Yeganeh_ht 9 ай бұрын
this was helpful thank you
@bawarkhalid2651
@bawarkhalid2651 2 жыл бұрын
BEST!
@kseniyavolkov6689
@kseniyavolkov6689 3 жыл бұрын
thank you!
@mikeafter5
@mikeafter5 3 жыл бұрын
Thanks
@mohitjain957
@mohitjain957 2 жыл бұрын
thanku sir
@victorrezende6002
@victorrezende6002 Жыл бұрын
Nice class
@Ri_sab55
@Ri_sab55 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 ?
@Izzy-tl3gp
@Izzy-tl3gp Ай бұрын
i am wondering the same thing!
@cantona0823
@cantona0823 2 жыл бұрын
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?
@blackvampiremovie
@blackvampiremovie 4 ай бұрын
thank you so much!
@cashionsteven5695
@cashionsteven5695 3 жыл бұрын
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 ?
@dimitriskarkavelias
@dimitriskarkavelias 2 жыл бұрын
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 Жыл бұрын
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
@tsaykostya
@tsaykostya Жыл бұрын
Wow nice channel bro❤
@ixer6661
@ixer6661 Жыл бұрын
is this method usable to unsorted array?
@bingusiswatching6335
@bingusiswatching6335 6 ай бұрын
No
@CodeWithEssa
@CodeWithEssa 6 ай бұрын
Love it!
@AHorseWithNoName-bs7sm
@AHorseWithNoName-bs7sm 10 ай бұрын
Thank you!
@akshayav1035
@akshayav1035 8 ай бұрын
good one bro!
@TheEvertonDias
@TheEvertonDias Жыл бұрын
Thanks, Bro!
@Talkatfive
@Talkatfive 8 ай бұрын
Nice
@rayhanrajhendra6128
@rayhanrajhendra6128 11 ай бұрын
it is a good video, nice, i like it papa
@anuarcool3275
@anuarcool3275 Жыл бұрын
cool
@folksypegasus9188
@folksypegasus9188 Жыл бұрын
Ha nice 👍🏿
@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; }
@ntombizakhexego-jimlongo8577
@ntombizakhexego-jimlongo8577 2 ай бұрын
thank you
@ٱسمر
@ٱسمر 3 жыл бұрын
thx
@ПауверТзен
@ПауверТзен Жыл бұрын
thanks a lot)
@rewardx
@rewardx 3 жыл бұрын
gucci gang gucci gang gucci gang gucci gang gucci gang gucci gang
@AbhijeetKumar-cm3jh
@AbhijeetKumar-cm3jh 3 жыл бұрын
Face reveal on 100k ?
@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 жыл бұрын
I would love to help, but I barely have any time with running this channel and my day job :(
@GamingPro-xn4hy
@GamingPro-xn4hy Жыл бұрын
Help the yt algorithm
@aka-Monster01
@aka-Monster01 9 ай бұрын
u r awesome
@slade8863
@slade8863 2 жыл бұрын
yo, random comment
@saicharan4212
@saicharan4212 3 жыл бұрын
👍
@Kerzyist
@Kerzyist Жыл бұрын
Ehhhh Mazing
Learn Interpolation search in 8 minutes ❓
8:24
Bro Code
Рет қаралды 44 М.
Learn Quick Sort in 13 minutes ⚡
13:49
Bro Code
Рет қаралды 389 М.
Ice Cream or Surprise Trip Around the World?
00:31
Hungry FAM
Рет қаралды 21 МЛН
This Game Is Wild...
00:19
MrBeast
Рет қаралды 152 МЛН
Introduction to Binary Search
16:25
Lalitha Natraj
Рет қаралды 158 М.
Learn Bubble Sort in 7 minutes 🤿
7:44
Bro Code
Рет қаралды 289 М.
Binary Search Algorithm | C Programming Example
11:41
Portfolio Courses
Рет қаралды 59 М.
Learn Big O notation in 6 minutes 📈
6:25
Bro Code
Рет қаралды 279 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 687 М.
you will never ask about pointers again after watching this video
8:03
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Binary search in 4 minutes
4:00
Michael Sambol
Рет қаралды 132 М.
Binary Search - A Different Perspective | Python Algorithms
8:56
Learn Binary search trees in 20 minutes 🔍
20:25
Bro Code
Рет қаралды 182 М.
Ice Cream or Surprise Trip Around the World?
00:31
Hungry FAM
Рет қаралды 21 МЛН