0

Everything in pset3 works well except two: check50 says:

:) helpers.c exists
:) helpers.c compiles
:) finds 42 in {42,43,44}
:) finds 42 in {41,42,43}
:) finds 42 in {40,41,42}
:) finds 42 in {41,42,43,44}
:) finds 42 in {40,41,42,43}
:) finds 42 in {39,40,41,42}
:( doesn't find 42 in {39,40,41}
   \ killed by server
:( doesn't find 42 in {39,40,41,43}
   \ killed by server

My code for binary search is:

int max=n-1;
int beg=0;
int end=n-1;

while(n>0)
{
    int mid=(beg+end)/2;

    if(values[mid]==value)
    {
        return true;
    }
    else if(values[mid]>value)
    { 

        for(int i=0;i<mid;i++)
        {
            if(values[i]==value)
            {
                return true;
            }
        }
    }
    else if(values[mid]<value)
    {

         for(int i=max;i>mid;i--)
         {
              if(values[i]==value)
            {
                return true;
            }
         }
    }
    else
    {
        return false;
    }

}

return false;
}

Please help me with this one.

1

There are a few problems with the code as far as I can see.

  1. Killed by server means that the server shut down your program for some reason. Most likely this is caused by an infinate loop or an unending recursive call.

  2. You can see from check50, that your code fails, where 42 is not among the numbers, so you got a problem with handling when the value is not present.

  3. What you have written, is not really binary search. The first step of it is, but then you just search the two sides lineary. You should try to continue the binary searching whithin the remaining section of your array.

I hope that helps enough to get you on the right track. Good luck!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .