2.6.2 Binary Search Recursive Method

2.6.2 Binary Search Recursive Method

Abdul Bari

6 лет назад

524,287 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Tridip Mukherjee
Tridip Mukherjee - 08.10.2023 17:07

Hello Sir, lots of respect to your teaching. I have implemented this in JS : please check :)
Thank You.


let arr = [3,6,8, 12,14,17, 25, 29, 31, 36, 42, 47, 53, 55, 62];

function recursive_binary_search(target, low, high) {
const mid = Math.round((low + high)/2);
if (arr[mid-1] === target) {
console.log(`Element Found In Position : ${mid-1}`);
} else {
if (low >= high || (mid >= arr.length)) {
console.error("Range Error: Element Not found!");
} else {
if (arr[mid-1] < target) {
return recursive_binary_search(target,(mid-1)+1, arr.length )
} else if (arr[mid-1] > target) {
return recursive_binary_search(target,1, mid-1)
}
}
}
}

recursive_binary_search(2, 1, arr.length)

Ответить
Vignesh Prabhu
Vignesh Prabhu - 25.09.2023 11:39

I love u sir you're my bahubali

Ответить
saivaraprasad mandala
saivaraprasad mandala - 06.09.2023 22:50

why isn't the order of binary search is O(log n to the base 2)

Ответить
NAMITA SANDIL
NAMITA SANDIL - 24.07.2023 16:42

wowww

Ответить
Akshay Sharma
Akshay Sharma - 03.07.2023 16:31

Sir, a function can return the function itself which you have done int binary search algorithm ???

Ответить
practice mail
practice mail - 29.06.2023 10:19

I'm a non-cs background student and even i can understand such simple explanation. Hats off to you sir for your contribution sir.

Ответить
lucky white
lucky white - 31.05.2023 08:56

Great to come across your clips on algorithm, you lecture just rolled away huge pressure off my neck. Thanks Sir

Ответить
Mohammad Faisal
Mohammad Faisal - 06.05.2023 07:55

i wish there was a coding part also. i Have NEVER seen a teacher with this level of understanding in data structures.

Ответить
Yan Min Thwin
Yan Min Thwin - 05.05.2023 14:55

Thank you soo much sir

Ответить
kyokibot
kyokibot - 10.04.2023 00:40

I know that my ex will always come here because she is studying algo now. I just wanna say, I miss you and I love you to the moon and back. I really do. Keep it up with your studies ok? ❤

Ответить
Saran
Saran - 13.11.2022 21:16

There is a slight mistake in your algorithm if(l==h) will throw you a stackoverflow error instead you should use if(h<=l).........

Ответить
Seng Keat
Seng Keat - 05.11.2022 00:17

Master what kinds of problems we can divide? like in case of the binary search above?

Ответить
FELIPE
FELIPE - 27.10.2022 09:50

I have just one thing to say: Thank you!

Ответить
WEIXIN WANG
WEIXIN WANG - 24.09.2022 06:02

That's the thought I was looking for, thanks

Ответить
coding vibes
coding vibes - 19.09.2022 16:53

Why are we using return in if else block..instead we can avoid return in if else block
I think return mid is sufficient

Ответить
Jayadev Pulaparty
Jayadev Pulaparty - 13.09.2022 13:21

One thing i observed is that if we take an even sized array, we are getting tree of depth 5 instead of log(16)= 4. Please let me know if my observation is valid

Ответить
Nacho Macho
Nacho Macho - 31.08.2022 15:09

👍👍👍

Ответить
Niti KT
Niti KT - 21.08.2022 01:09

Bari Sir, hats off to you. I brag to my friends that I was your student and thought me DSA during my engineering and every time I have an interview I make sure I go through some of your videos..

Ответить
Shirin Nurliyeva
Shirin Nurliyeva - 19.06.2022 07:17

cannot express my gratitude. Thank you Mr Abdul - all of your videos are priceless! you're a legend. Thank you for helping to improve

Ответить
Shirin Nurliyeva
Shirin Nurliyeva - 19.06.2022 07:09

Thank you Mr Abdul - all of your videos are priceless! you're a legend. Thank you for helping to improve

Ответить
Md Ayan Ali
Md Ayan Ali - 14.06.2022 21:19

31st video

Ответить
Mustafa Mahmoud Ahmed
Mustafa Mahmoud Ahmed - 19.04.2022 19:15

Thanks for this helpful playlist, you are the best.

I tried to implement the algorithm as it is in the video, it didn't work except for values already in the array.

My recommendation:
1- remove the if block
2- Modify the else block to be "if (l <= h)"
3- Add else block which returns -1 in case "l > h"

Here is my implementation in C++:

int BinSearch(int l, int h, int key,vector<int> arr) {
int mid;
if (l <= h) {
mid = (l + h) / 2;
if (key == arr[mid])
return mid;
else if (key < arr[mid])
return BinSearch(l, mid - 1, key, arr);
else
return BinSearch(mid + 1, h, key, arr);
}
else
return -1;
}

Finally, I want to thank you for your effort in this series which really helps me to learn from scratch.

Ответить
Mohammed Adel
Mohammed Adel - 16.04.2022 08:54

Thank you for your hard work.

Ответить
Shobhit Lamba
Shobhit Lamba - 07.03.2022 23:08

why aren't we using "while(h>=l)" in the else statement?

Ответить
Jonelle Yu
Jonelle Yu - 23.02.2022 02:47

So helpful! Thank you Sir! Btw would you please enable the subtitles? My english is not very good. Thank you.

Ответить
Ihsan Nurul Iman
Ihsan Nurul Iman - 21.02.2022 14:31

did he forget to input the array in the parameter?

Ответить
Laxmi G
Laxmi G - 02.02.2022 10:33

Explain examples sir

Ответить
Coleman Nelson
Coleman Nelson - 19.01.2022 05:16

Made this very easy to understand, divide and conquer!

Ответить
chaosavenger2
chaosavenger2 - 19.01.2022 02:05

you'd also have to check if your lower bound is greater than you upper bound, such as for the input [2,5], target = 0

Ответить
deepak1291
deepak1291 - 07.01.2022 12:26

Algo worked fine when item is inside the array or greater then the array elements but fails, when item is less then the entire array elements.

Pass:

1-> When item is present
2-> When item is greater then all the items present

Failed: When Item is less then all the items

int[] a = {2, 3, 4, 10, 40};
int searchItem = 1;
int low = 0;
int arraySize = a.length;
int high = arraySize - 1;

private static int search(int[] a, int low, int high, int searchItem) {
if (low == high) {
if (a[low] == searchItem)
return low;
else
return -1;
}
else {
int mid = (low + high) / 2;
if (searchItem == a[mid])
return mid;
if (searchItem < a[mid] )
return search(a, low, mid-1, searchItem);
else
return search(a, mid+1, high, searchItem);
}
}


Condition need to be added :

Instead of: if (low == high) {
we need to put one more check there:
if (low >= high) {

Ответить
Lê Thanh Châu
Lê Thanh Châu - 01.01.2022 06:35

I can't see the method where you find the first occurrence of the element to look for and what if your array r<l, hic

Ответить
abcdfghijlnopqrst
abcdfghijlnopqrst - 24.11.2021 05:36

Thank you for these videos Abdul Bari. I normally struggle to learn from videos, but your concisesness helps so much. I know that my focus won't be wasted, so I can listen intently for the full video.

Ответить
Mohamed Atef
Mohamed Atef - 21.11.2021 16:35

This is, by far the best tutorial for learning binary search I've ever seen.
Many people have sufficient knowledge and skills and they try passing them to others
but a few of them can deliver them in a convenient method like Abdul Bari Does.

Ответить
Gayatri Barhate
Gayatri Barhate - 13.08.2021 09:11

God bless you and your loved ones..... Keep rising... 🥰😘

Ответить
Kevin N
Kevin N - 11.08.2021 00:27

Amazing!!!

Ответить
Aditya Singh
Aditya Singh - 28.06.2021 00:16

Sir, how is BinarySearch Divide and Conquer? We are not dividing the problem into multiple sub-problems. We are, at every step, decreasing the problem into a single smaller problem. We don't divide the array into 2 halves at each stage and process both arrays. I read on internet it is a Decrease and Conquer problem. Is this true?

Ответить
Amber chawla
Amber chawla - 14.06.2021 17:05

Sir i dont think l > h , case is covered which actually could occur

Ответить
ivanwhythen
ivanwhythen - 06.06.2021 04:34

This is a truly great video, but wouldn't we want to include the sorted array as one of the parameters for the recursive binary search?

Ответить
1977 Dark age begins
1977 Dark age begins - 14.02.2021 19:08

You are great❤️❤️

Ответить
Intuitive J
Intuitive J - 08.02.2021 08:24

I can finally write the code after this lecture series! Thank you from South Korea.

Ответить
integral merr
integral merr - 04.02.2021 15:10

ofc the microphone is clipping...

Ответить
ZULFIQAR SYED
ZULFIQAR SYED - 22.01.2021 19:56

Thank you x 100

Ответить
Zureka
Zureka - 20.01.2021 17:46

Sir in your recursive code, you didn't give a condition for when the element can't be found, i.e. you have to give a condition that when l>h return -1. Other than that amazing lectures

Ответить
Subramaniyan VG
Subramaniyan VG - 26.12.2020 11:46

Sir you should be guiding students for companies like google and amazon. Also please write a book on Data Structures definitely I will buy.

Ответить
aaron aaron aaron
aaron aaron aaron - 11.11.2020 16:38

🤯

Ответить
Rachit Singh
Rachit Singh - 07.11.2020 19:55

Great explanation

Ответить