ThePrimeagen / kata-machine

1.22k stars 1.22k forks source link

Pseudo Code Binary Search lesson: v > m #16

Open prb01 opened 1 year ago

prb01 commented 1 year 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 1 year 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 2 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 2 months ago

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

Yes, that does make sense! -James