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...
=========================================================== - วิทยาศาสตร์และเทคโนโลยี
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;
}
}
Could you please make an in depth video on Binary Search Trees including how to balance them ?
Feridusssssssssss
thanks heres a comment cause you asked
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.
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!!!!
Had a task with binary search.
Didn't understand -> open Bro Code Data Structures and Algorithms Playlist -> Found Binary Search -> Understood :D
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
commenting for the algorithm
love your stuff my guy 👍
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.
This example gave me a much better understanding of how searching works.
Thank you so much for making such a great content
You saved the day, AGAIN !
Thanks so much bro, take care of you ! :)
this channel should have more than a million subscribers!!!
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.
A hi from "Brazil' Bro, code! You are the guy.
Great explanation, thanks a lot!!
Very well explained. Simple and understandable!
You are underrated af
For Sure
@@niksonney4462 Yeah man I did ask my friends to subscribe him :)
thank you. Your video helped a lot.
Thank you so much Lord Bro Code. I was able to finish my Java final semester project thanks to you sir.
well, you're an absolute legend
that's all I got to say
Your way of teaching is just fantastic bro🤩. You are the best. Love from India❤
Amazing video bro, Thank You so much.
Great video, thanks bro.
Bro you are genus !😍😍😍
Here I was over complicating it in my head thank you!
You are the best!💻
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...
am officially done with "programming" but I will keep my subscription ❤
You are pathetic.
Thank you. Such a helpful video
Nice and clear as always.
I hope that you can do some tutorials about every data Structure or techniques... I love they way you teach
@xavier from alternate universe be like
Well done, thank you a lot bro.
Thank you, bro code! Excelent explaining.
Thanks bro, this is awesome
this was helpful thank you
good job bro....!
Great methodology 🎉 thx
Nice tutorial
Love it!
Awesome!
You Know One Thing!..... You Are My Bro.....
Better than any movie.
thank you!
Thank you!
Dude is the GOAT fr
good one bro!
Great vid
Thanks bro
*ongoing comment crusade while i cram*
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?
So underrated....Bro keep goin❤...
I know u hate it but waiting for ur OP android app development lol
Thank you, sir 👍👍👍
go super Bro!
Thanks, Bro!
Thanks!
Thank you.
... can you literally just teach me everything, math / science / life..... lol you vids are so clear and easy to understand
So much knowledge
Wow nice channel bro❤
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
and maybe heaps
shout out from South Korea
Nice class
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).
Thanks
thanks a lot)
BEST!
Nice
good explanation but explain the while loop part too and why we are using middle +1 and middle -1 for effective understanding
Hey bro, please make video of react js or angular 🤩
Please also solve some leetcode problems
thx
it would be nice a recursive explanation also.
it is a good video, nice, i like it papa
thanku sir
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
u r awesome
cool
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.
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
Binary search is one of the easiest and most efficient searches for anything that is in ascending or descending order.
bro code is bro god after this
Ha nice 👍🏿
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 ?
👍
❤
why everyone finds middle with complex formula, instead of (low+high)/2, while both give same result???
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.
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.
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)
Ehhhh Mazing
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).
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
♥
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?
fellow bro
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 ?
gucci gang gucci gang gucci gang gucci gang gucci gang gucci gang
Face reveal on 100k ?
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.
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
@@BroCodez No it won't work and the value of middle will repeat itself again and again...
It will stuck in a constant loop
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.
yeah middle = (low+high)/2; is work, replace the formula that bro use. You see the same result!!! happy coding💲💲
is this method usable to unsorted array?
No
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?
$ey bro
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
I would love to help, but I barely have any time with running this channel and my day job :(
Help the yt algorithm
Feriduussssssss
yo, random comment
Y