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...
===========================================================
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
Had a task with binary search.
Didn't understand -> open Bro Code Data Structures and Algorithms Playlist -> Found Binary Search -> Understood :D
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!!!!
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
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
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.
You are underrated af
For Sure
@@niksonney4462 Yeah man I did ask my friends to subscribe him :)
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
Your way of teaching is just fantastic bro🤩. You are the best. Love from India❤
You do an outstanding job of making the content easy to understand. You have helped me get through my class this quarter thank you!
This example gave me a much better understanding of how searching works.
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.
well, you're an absolute legend
that's all I got to say
Thank you so much Lord Bro Code. I was able to finish my Java final semester project thanks to you sir.
this channel should have more than a million subscribers!!!
commenting for the algorithm
love your stuff my guy 👍
am officially done with "programming" but I will keep my subscription ❤
You are pathetic.
How are u "done" lol
@@dazzledaveWell i guess he quiet or something
You Know One Thing!..... You Are My Bro.....
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...
Thank you so much for making such a great content
A hi from "Brazil' Bro, code! You are the guy.
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
Better than any movie.
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
Bro you are genus !😍😍😍
good explanation but explain the while loop part too and why we are using middle +1 and middle -1 for effective understanding
Here I was over complicating it in my head thank you!
So underrated....Bro keep goin❤...
I know u hate it but waiting for ur OP android app development lol
Amazing video bro, Thank You so much.
You saved the day, AGAIN !
Thanks so much bro, take care of you ! :)
you're the organic chem tutor of coding
thank you. Your video helped a lot.
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).
Great video, thanks bro.
You are the best!💻
You are smoother than Bro Code Liquor
Thanks bro
*ongoing comment crusade while i cram*
Great explanation, thanks a lot!!
Very well explained. Simple and understandable!
... can you literally just teach me everything, math / science / life..... lol you vids are so clear and easy to understand
Thank you, bro code! Excelent explaining.
Please also solve some leetcode problems
Hey bro, please make video of react js or angular 🤩
Nice tutorial
good job bro....!
Thanks bro, this is awesome
Dude is the GOAT fr
Well done, thank you a lot bro.
it would be nice a recursive explanation also.
Thank you. Such a helpful video
Nice and clear as always.
shout out from South Korea
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
what a legend ,watched his vid 5 days b4 exam and got a 97/100
Great methodology 🎉 thx
why not use low+high/2, in simple it works fine, but low and high values are large, then integers might overflow
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 much knowledge
this was helpful thank you
Great vid
Thank you.
Thank you, sir 👍👍👍
Thanks!
go super Bro!
thank you so much!
Awesome!
Thank you!
Love it!
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?
Thanks, Bro!
Wow nice channel bro❤
BEST!
Thanks
good one bro!
thanku sir
bro code is bro god after this
Nice class
thank you
why everyone finds middle with complex formula, instead of (low+high)/2, while both give same result???
it is a good video, nice, i like it papa
thanks a lot)
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
Ha nice 👍🏿
thx
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 ?
i am wondering the same thing!
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💲💲
cool
u r awesome
is this method usable to unsorted array?
No
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 ?
Face reveal on 100k ?
gucci gang gucci gang gucci gang gucci gang gucci gang gucci gang
👍
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?
❤
Ehhhh Mazing
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;
}
♥
sweet