Introduction to Binary Search (Data Structures & Algorithms #10)

Introduction to Binary Search (Data Structures & Algorithms #10)

CS Dojo

3 года назад

245,214 Просмотров

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


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

Waisetsubunsho
Waisetsubunsho - 02.10.2023 10:48

Why is there no condition for when target is < left or > right? Or just plain =left or =right?

Ответить
Aldrin John Encina
Aldrin John Encina - 06.08.2023 06:55

thank you...

Ответить
David Lee
David Lee - 19.07.2023 09:16

thank you

Ответить
Naginda Aisha
Naginda Aisha - 12.07.2023 23:23

Help me give me your number

Ответить
Boomni
Boomni - 14.05.2023 20:31

You're great. The explanations are really broken down. Thank you👍

Ответить
Garrett Smith
Garrett Smith - 21.01.2023 16:39

Lots of verbal pauses, uhhh, make for difficult listening, ahh

Ответить
Kevin Garcia
Kevin Garcia - 30.12.2022 06:09

Thank you for this video. That was a really clear explanation

Ответить
Nation Hlohlomi
Nation Hlohlomi - 17.10.2022 22:14

Great video as always

Ответить
MevansCodes
MevansCodes - 08.09.2022 06:11

I know this is old but what software were you using to create this ?
Awesome video by the way. I was having trouble understanding the pointer aspect on codecademy but seeing the visuals helped a lot

Ответить
Ajay Jangra
Ajay Jangra - 05.09.2022 06:10

thanks a lot!

Ответить
Lucas Mendonça
Lucas Mendonça - 22.08.2022 18:28

The link does not work anymore, maybe you can add a new one in the video description. Thank you

Ответить
Deep Joshi
Deep Joshi - 05.08.2022 17:06

# This is code for rotated array. First we define function for rotate array in particular direction then we give input that rotated array in Binary search code.
# If the code will help you please like the comment.
def rotate_ntime(arr,direction, n):
if direction=="Left":
for i in range(n):
left_rotate_one(arr)
if direction=="Right":
for i in range(n):
right_rotate_one(arr)

return arr

def left_rotate_one(arr):
temp=arr[0]
for i in range(len(arr)-1):
arr[i]=arr[i+1]
arr[len(arr)-1]=temp
return arr
def right_rotate_one(arr):
temp=arr[len(arr)-1]
for i in range(len(arr)-1,0,-1):
arr[i]=arr[i-1]
arr[0]=temp
return arr

arr=[8,9,11,13,-2,3,4,7]
arr=rotate_ntime(arr,"Left",4)
print(arr)

def Binary_search(arr,target):
low=0
high=len(arr)-1
while low<=high:
mid = (low + high) // 2
if arr[mid]==target:
return mid
elif arr[mid]<target:
low=mid+1
else:
high=mid-1

print(Binary_search(arr,11))

Ответить
Ankit podal
Ankit podal - 04.06.2022 13:39

no i dont wanna be a software engineer at google..
i hate you algo expert.
okay bye

Ответить
Dev Raj
Dev Raj - 15.04.2022 03:25

Clean explanation! Thank you so much!

Ответить
EDS
EDS - 11.04.2022 15:53

Nice Dojo

Ответить
Steam Acc
Steam Acc - 21.03.2022 03:11

What app do you use to draw these examples?

Ответить
Unknown Unknown
Unknown Unknown - 06.03.2022 15:48

why u gotta check n/2 elements
its a computer always starts from 0 to n numbers

search(array, the value u r searching for)

so where did the n/2 come from ?
is it something i should just memorize ?

Ответить
Mans Mor
Mans Mor - 06.02.2022 14:10

I can't not to like your videos, using technology and explaining in a simplest way makes it easy to understand even very difficult subjects.

Ответить
Mikkeline Elleby
Mikkeline Elleby - 23.01.2022 20:55

Hi you implemented to binary search wrong, how you calculate mid will can give you an overflow - you should correct it to this : mid = left + (right - left) /2
Maybe point this out so you don't teach something wrong to other students.
But else great video !

Ответить
Chukwuemeka Stanley
Chukwuemeka Stanley - 12.01.2022 03:24

do you have a tutoral on Hash maps, Stacks/Queues/Heap/Priority Queue ?

Ответить
Adib Attie
Adib Attie - 27.11.2021 13:50

It's not obvious how much binary search helps until you actually code it and time it yourself. It's actually amazing. Thanks for the informative videos!

Ответить
Nagendra Devara
Nagendra Devara - 24.11.2021 15:52

I have tried the python example in JavaScript

function binarySearch(arr, target) {
let left = 0;
let right = arr.length - 1;
arr = arr.sort((a, b) => a - b)
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] == target) {
return mid
} else if (target < arr[mid]) {
right = mid - 1
} else {
left = mid + 1
}
}
return -1
}


binarySearch([2, 1, 13, 3, 10, 8, 6, 0, 12,16], 8)

Ответить
Vulnerable Growth
Vulnerable Growth - 12.11.2021 04:16

Ok, so binary search only works if the list is sorted or sorted+shift, correct?

But, I then I have a question. Why not check if the target is more or less than the range of values before starting the loop? We just assume that’s less likely to happen so we’d rather go straight to checking targets which can be found first? As in, “hey, if you sent us a target that doesn’t fit in the list, you’re gonna have to wait.”

Ответить
spacewitch
spacewitch - 03.11.2021 08:01

your videos are so helpful, dude. tysm! ;u;

Ответить
Sefty Yunitasari
Sefty Yunitasari - 02.11.2021 05:46

Thank you so much, you helped me a lot!

Ответить
IT Engineer Emmanuel
IT Engineer Emmanuel - 30.10.2021 23:51

The arr[mid] don´t accept float numbers, let's say we an array of 8 elements which means that the right value will be 7 (len(arr) - 1), so the division is 7/2 = 3.5 and that's a float number which allowed in mid. I round the value in python, but it gets me a "None" answer when a I use arrays like [1,3,5,7,9,11,13,15], what you suggest me to do?

Ответить
Gilbert Ong
Gilbert Ong - 29.10.2021 00:06

actually if the target is 12, the search would end with left being 7 and right being 6.
Not left being 6 and right being 5.
But whatever. :P
Cool stuff

Ответить
Dev G
Dev G - 15.10.2021 22:43

thanks fam <3

Ответить
islam kystaubay
islam kystaubay - 13.10.2021 19:44

Thank u a lot!

Ответить
nique
nique - 13.10.2021 12:14

THANK YOU! for saving me always. Your tutorials are easy to understand compared to others. Great teacher!

Ответить
Mr. Eric
Mr. Eric - 05.09.2021 16:39

Thank You!!

Ответить
Divesh Kosuri
Divesh Kosuri - 05.09.2021 12:08

You just made it so simple!
Thanks

Ответить
Jotaro Kujo
Jotaro Kujo - 28.08.2021 19:47

what if the value youre looking for is in L or R?

Ответить
Biblical Archaeology - علم الآثار والكتاب المقدس
Biblical Archaeology - علم الآثار والكتاب المقدس - 18.08.2021 17:17

Thank you!!!

Ответить
Kama,Love & Romance
Kama,Love & Romance - 10.08.2021 17:45

Please HELP
Why should we use array.length -1 as the index in Python is from 0 to 9 but using range (0,(array.length) will EXCLUDe last number from the calculations.


Thanks in Advance!!!!!

Ответить
Anum
Anum - 09.08.2021 23:51

best lecture👌🏻

Ответить
⚽..فريق الأبطال.. 💪
⚽..فريق الأبطال.. 💪 - 08.07.2021 10:22

Great explanation thanks

Ответить
Haddon James
Haddon James - 04.07.2021 00:52

great explanation

Ответить
thota teja
thota teja - 03.07.2021 10:26

People who trying to get into google .
Is python usefull to get into google

Ответить
Khaled Hafsaoui
Khaled Hafsaoui - 25.06.2021 23:54

Thank you sirr👏👏👏

Ответить
Wisdom Over Money
Wisdom Over Money - 25.06.2021 13:03

You made it so simple.

Ответить
Shreya Pandey
Shreya Pandey - 19.06.2021 10:50

such a good explanation!

Ответить
2Calam
2Calam - 17.06.2021 06:26

I'm very new to this and have watched a few of these videos and found them interesting.

What I don't understand though, is what the arrays and targets may relate to? Like what industry problem could be solved by these methods?

If someone could explain that or point me in the right direction would be appreciated.

Ответить