Binary Search - Recursive implementation

  Рет қаралды 164,608

mycodeschool

mycodeschool

Күн бұрын

Пікірлер: 67
@leoniduvarov6565
@leoniduvarov6565 7 жыл бұрын
I cant put into words how helpful this was. Keep it up!!
@mycodeschool
@mycodeschool 11 жыл бұрын
You're welcome Aaron !!
@jeanphilius7500
@jeanphilius7500 7 жыл бұрын
Great explanation Animesh! If anyone is confused by why Animesh switched to formula mid = (high - low)/2 + low 1:04 Here's how I was able to wrap my head around it. lets PRETEND anything over 10 causes an OverFlowException. We will set low and high arbitrarily to 4 and 8 respectively (e.g., low = 4, high = 8). Therefore, in our contrived example, if we sum low + high we get an OverFlowException, because 4 + 8 = 12 which is greater than 10 (the highest number we can handle before overflowing). This is why using formula mid = (high + low) /2 breaks down. A more robust formula would be mid = (high - low)/2 + low .... And if we plug in values from our contrived example to calculate mid = (high-low)/2 + low = (8-4)/2 + 4 = 6 we see that it gives us the same answer as mid= (high + low)/2 = (8+4)/2 = 6 but without causing an OverFlowException!
@spacetime_wanderer
@spacetime_wanderer 6 жыл бұрын
Good example. I would initially prefer 'mid = floor((high/2) + (low/2));' at least till I remember low + (high-low)/2. The disadvantage in my way I feel is division is slightly more costly than addition and subtraction - and I am doing that twice.
@cheems08213
@cheems08213 5 жыл бұрын
thank you
@SaurabhMishra0709
@SaurabhMishra0709 4 жыл бұрын
Thanks for the explanation J Phillius
@095_shaniabalkhi9
@095_shaniabalkhi9 3 жыл бұрын
Thanks bruh!
@kainatmalik1752
@kainatmalik1752 4 жыл бұрын
thank you so much, sir, for your easy and such attractive way of teaching, I never feel bore during listening to your lecture.
@manuelconte623
@manuelconte623 3 жыл бұрын
this video was since 2013 and in 2021 this still good!!!
@agniveshadhikari
@agniveshadhikari 11 жыл бұрын
Crystal clear explanation
@AllRounder-q9g
@AllRounder-q9g 7 жыл бұрын
My man you are so convincing, truely amazing......
@miguelangelguzmansanchez6286
@miguelangelguzmansanchez6286 9 жыл бұрын
you´re da fuckin bomb dude, ¡viva la indiaaaaa!
@nagenHARP
@nagenHARP 2 жыл бұрын
farrrrrrrrrrrr better than current channels .
@muneerahmad5545
@muneerahmad5545 4 жыл бұрын
Very good bhaiya easily explained very good.keep it up
@ShekharKumar-tn9rl
@ShekharKumar-tn9rl 2 жыл бұрын
I know You are no More Sir But seriously I am saying a big thanks to you I haven't found any teacher like you till date you are best of all time sir 🙂 Nation needs Teacher Like You Sir RIP OM Shanti
@hamiltonian450
@hamiltonian450 6 жыл бұрын
thanks for the tutorial it helped a lot keep up the great work!!
@harryy000
@harryy000 9 жыл бұрын
Question though.For the -1 case meaning when the element is not present in the array since the array is sorted and we know the size of the array,would it not be wiser to initially check if the element we are searching for is gt the size of the array or less than the value in the minimum index(arraypos[0]).Won't this be more effective than doing a if low > high then return -1 which will only be true after completing a recursive/iterative exhaustion of the array?
@nafisfaiyaz7543
@nafisfaiyaz7543 6 жыл бұрын
no, because the key we are searching for can be inside the range of the lowest and highest elements in the arrey and still not present in the arrey. so, if you check only for lowest and highest elements, chances are you will not find the -1 case
@scienceisinaccordancewithi3688
@scienceisinaccordancewithi3688 6 жыл бұрын
YOUR VIDEOS ARE REALLY AWESOME SIR THANKYOU VERY MUCH KEEP UP THE GOOD WORK
@095_shaniabalkhi9
@095_shaniabalkhi9 3 жыл бұрын
Great tutorial. Thanks!
@adityaroshanpatro9861
@adityaroshanpatro9861 4 жыл бұрын
why does that -1 return to its previous one till the top ?? 7:41
@utpalbandyopadhyay1633
@utpalbandyopadhyay1633 4 жыл бұрын
Great explanation ......!!😊👍
@SoundscapesbyPritam
@SoundscapesbyPritam 9 жыл бұрын
Why not u have used "while loop" as a base condition in recursive approach? will it be true if we use "while" instead of "if" as the base condition?
@lekanosagie
@lekanosagie 9 жыл бұрын
+Pritam Why use recursion if you're still planning on using loops?
@unboxwithaakash
@unboxwithaakash 8 жыл бұрын
if loops were to use in recursion then program would gud if remain iterative itself.
@ishaankulkarni49
@ishaankulkarni49 4 жыл бұрын
Thanks you so much for making this!!!
@krishanudutta2943
@krishanudutta2943 3 жыл бұрын
Thanks a lot sir for this amazing lecture...
@chinmayrath8494
@chinmayrath8494 7 жыл бұрын
Best explanation. Thanks a lot !
@abhinavtyagi1657
@abhinavtyagi1657 3 жыл бұрын
why do we need return keyword inside our if and else if statements ?? Bcoz even if we don't return, we still calling the same function i.e. making our recursive call.
@JuanRodriguez-fk2iy
@JuanRodriguez-fk2iy 4 жыл бұрын
Hello! Amazing video!! I wanted to ask, which method takes less memory? I am still having trouble understanding how recursive functions use memory. Thank you!
@075_ritikkumar7
@075_ritikkumar7 4 жыл бұрын
Hey bro. Watch his videos under the playlist recursion. I am sure your doubts would be cleared.
@shaikshabbir4479
@shaikshabbir4479 4 жыл бұрын
can we also exit when low == high ?
@rohanmahajan3436
@rohanmahajan3436 Жыл бұрын
when low == high, then we have to check if A[mid] == x. If that holds true, then we would return mid. Otherwise, if that is false, we return -1. This is still correct, just another way to code it.
@ankitthakar2671
@ankitthakar2671 3 жыл бұрын
We should avoid recursion because sometimes it takes lots of stack space. specifically if you are working in the domain where performance factor is the key.. Your videos are really good.
@sangeethapratapan1904
@sangeethapratapan1904 6 жыл бұрын
thank you Sir. It helped me a lot.
@mhm5162
@mhm5162 2 жыл бұрын
superb,,good job
@real_manikant
@real_manikant 8 жыл бұрын
Sir , what is the Space complexity of Binary search in the recursive implementation ????????????? I think it should be O(1) but many people argue that its O(log n)............. Please reply soon
@gluck91
@gluck91 7 жыл бұрын
O(1) is constant and you do not find solution in one method call, so in this case is O(log) because every time you call method you divide the problem in 2.
@jahnaviization
@jahnaviization 7 жыл бұрын
Can you please do a tutorial on binary search using pointers on a list of names?
@gluck91
@gluck91 7 жыл бұрын
Good video but not every recursion method can be written in an iterative method, for example : Hanoi Tower problem
@gabriellashebly
@gabriellashebly 4 жыл бұрын
what does the arrow mean?
@AnilKumar-jp1cu
@AnilKumar-jp1cu 9 жыл бұрын
Thank you so much......vary nice approach
@amarvishwakarma9173
@amarvishwakarma9173 8 жыл бұрын
great explanation bro
@MexicanRmz
@MexicanRmz 7 жыл бұрын
Thanks bro, great explanation.
@hitrahulmehta
@hitrahulmehta 11 жыл бұрын
beautifully explained! thanks a lot!
@whiskybhoot
@whiskybhoot 10 жыл бұрын
OH WOW
@strawbebbie5
@strawbebbie5 11 жыл бұрын
so helpful! thanks!
@qwe123727
@qwe123727 8 жыл бұрын
In your code, Is this correct? mid = low + (high-low)/2;
@ankits386
@ankits386 8 жыл бұрын
it is correct ..if u just take tee L.C.M. the value of mid = (low+high)/2 .. mid = low + (high-low)/2 mid= (2low + high -low)/2 mid= (low + high)/2
@tenzin8773
@tenzin8773 7 жыл бұрын
Thank you sir!!
@ramankr0022
@ramankr0022 Жыл бұрын
very helpful.
@pratikshinde3402
@pratikshinde3402 5 жыл бұрын
start at 3:00
@keshavmaheshwari521
@keshavmaheshwari521 5 жыл бұрын
how can low be greater than high
@Manish-dh8gx
@Manish-dh8gx 4 жыл бұрын
When we don't find the item in the array our loop increments this happens - (low and high are on same index ) and for loop increments the low by 1
@brunocardoso8277
@brunocardoso8277 6 жыл бұрын
Thank you man!
@AnonymousDev1
@AnonymousDev1 Жыл бұрын
I'm a sensitive man . I see recursive function; I panic ! 😵‍💫
@soulasscape
@soulasscape 6 жыл бұрын
sir why do we write return with every if else case, we want to sub divide the array and call it recursively so cant we write bs(a,x,mid+1,end) instead of return bs(a,x,mid+1,end) ? i am not able to understand why we write return statement with every recursive call made kindly explain
@yahyashaikhworld
@yahyashaikhworld 8 жыл бұрын
Thanks , but iterative is easier to write
@saidbarke6363
@saidbarke6363 9 жыл бұрын
thank you.
@Crywolf203
@Crywolf203 11 жыл бұрын
thanks!!!
@kshitijvengurlekar1192
@kshitijvengurlekar1192 7 жыл бұрын
Thanks
@vaishnavi5070
@vaishnavi5070 7 жыл бұрын
please do this using pointers
@RAHUL-gf3ft
@RAHUL-gf3ft 3 жыл бұрын
Jai IIIT ALLAHABAD
@himanshu2487
@himanshu2487 7 жыл бұрын
please speak in your natural tone its array not aare its very irritating btw its very help video to me
@siddharthadas86
@siddharthadas86 7 жыл бұрын
you are an idiot.
@mithujana7241
@mithujana7241 6 жыл бұрын
SHUT UP
Binary search - finding first or last occurrence of a number
10:27
mycodeschool
Рет қаралды 356 М.
What is binary search
12:45
mycodeschool
Рет қаралды 707 М.
СИНИЙ ИНЕЙ УЖЕ ВЫШЕЛ!❄️
01:01
DO$HIK
Рет қаралды 3,3 МЛН
REAL or FAKE? #beatbox #tiktok
01:03
BeatboxJCOP
Рет қаралды 18 МЛН
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,3 МЛН
2.6.2 Binary Search Recursive Method
7:11
Abdul Bari
Рет қаралды 585 М.
Binary Search in Java - Full Simple Coding Tutorial
17:48
Coding with John
Рет қаралды 135 М.
Towers of Hanoi: A Complete Recursive Visualization
21:13
Reducible
Рет қаралды 516 М.
Learn Binary Search in 10 minutes 🪓
10:04
Bro Code
Рет қаралды 137 М.
Understanding B-Trees: The Data Structure Behind Modern Databases
12:39
Check if a binary tree is binary search tree or not
16:30
mycodeschool
Рет қаралды 382 М.
Recursion and Stack - English
13:09
Abdul Bari
Рет қаралды 190 М.
СИНИЙ ИНЕЙ УЖЕ ВЫШЕЛ!❄️
01:01
DO$HIK
Рет қаралды 3,3 МЛН