Binary Search: Two implementations basically the same but only one works. Why?

Binary Search: Two implementations basically the same but only one works. Why?



This is the question



After getting stuck I tried typing it in java. It failed to work. Here is the code:


public static boolean run1(int data, int x)
if(data.length == 0)
return false;
else if (data.length ==1)
if(data[0]==x)
return true;
else return false;

else
int m = (data.length)/2;
if(x<=data[m])
int left = Arrays.copyOfRange(data, 0, m+1);
return run1(left,x);
else
int right = Arrays.copyOfRange(data, m+1, data.length);
return run1(right,x);






But after trying a bunch of things, I made a change that made it work for all cases.


public static boolean run(int data, int x)
if(data.length == 0)
return false;
else if (data.length ==1)
if(data[0]==x)
return true;
else return false;

else
int m = data.length/2;
if(x<data[m])
int left = Arrays.copyOfRange(data, 0, m);
return run(left,x);
else
int right = Arrays.copyOfRange(data, m, data.length);
return run(right,x);







Why would it work for the second one but not the first? they both are essentially doing the same thing?



I suspect it to be something with the Len(A)/2, The first how miraculously works perfectly when I change the division statement to (len(A) - 1)/2, kinda makes sense because Len(A) - 1 = 0 + Len(A) -1 (First index + last index)



Edit: im NOT asking for the answer to the question on the paper where I found this code. Just an answer to how the change I made to the code ended up making it work





Question image says "Final Exam". Lol. I guess that is not homework then?
– Vaccano
2 days ago






I mean its a valid question! I was doing prep for an exam and came across this question. Im not asking people to solve that exam Question (i.e solve the proof for me) I'm just asking for help to understand why the variation I wrote worked while the one in the question didnt
– Iman Kewalramani
2 days ago





It also says its from 2014, so most likely preparation for the exam.
– Pinkie Swirl
2 days ago





(regarding the question) seeing that the syntax of the printed code is very close to python, this is what i think is happening... after calculating m, the code recurses bin_search() until A[m] which in most languages does not include the actual element at index m... and the second case only recurses until len(A)-1.. this is my best guess at this point
– Abby
2 days ago






@ImanKewalramani Any kind of studying is "homework", even if self-assigned, and you start out lying in the title ("Not homework"), so why would we be encouraged to help you? Fix the question so you don't try to deceive us into helping under false pretenses.
– Andreas
2 days ago









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

Henj