ThePrimeagen / kata-machine

1.28k stars 1.27k forks source link

Pseudo Code Binary Search lesson: v > m #16

Open prb01 opened 2 years ago

prb01 commented 2 years ago

Not sure if this is the right place to raise this but FrontendMasters suggested raising an issue in the repo.

In the Pseudo Code Binary Search lesson, shouldn't the else if condition specify n > v rather than v > m, where n = needle, v = value at midpoint, m = midpoint?

image

Thanks!

echosonusharma commented 2 years ago

@prb01 if the needle (n) is ahead of the middle value (m) then low should be mid + 1. there's some mess up in the sudo code....ignore that.

if (value === needle) {
    return true;
} else if (needle > value) {
    low = mid + 1;
} else {
    high = mid;
}
jameszenartist commented 1 year ago

@prb01 In addition, I recently noticed that he had set the initial value for hi as array.length. I've been looking for some good refreshers on algorithms lately, and I was always under the impression that it should be array.length - 1? Does that not matter?

-James

Agent-E11 commented 3 months ago

@jameszenartist I know this is over a year old, but I have an answer 🤣

hi is exclusive, so the code does not check the value at hi, only the values below hi

jameszenartist commented 3 months ago

Hi @Agent-E11 , thank you for your reply!

Yes, that does make sense! -James