1

As my question is titled, I would appreciate help with my binary search, shown below. Currently, the search will always return false. I am almost certain that my sorting algorithm is correct, as it worked perfectly while temporarily implementing a linear search. I have seen lots of other questions with similar problems as mine, yet I cannot spot the error in my code.

    #include <cs50.h>
    #include <stdio.h>
    #include "helpers.h"

    /**
     * Returns true if value is in array of n values, else false.
     */
bool search(int value, int values[], int n)         
{
        //TODO: implement a searching algorithm
int counter1 = 0;
int counter2 = n - 1;

while (counter2 >= counter1)
{
    int counter3 = (counter1 + counter2) / 2;

    if (values[counter3] == value)
    {
        return true;
    }
    else if (values[counter3] > value)
    {
        //search left
        counter2 = counter3 - 1;
    }
    else if (values[counter3] < value)
    {
        //search right
        counter1 = counter3 + 1;
    }
}
return false;
}

Here's my sort, just in case:

void sort(int values[], int n)

{


int EXIT = 0;

int counter3 = -1;

while (EXIT == 0)
{
    int counter1 = -1;
    int counter2 = 0;
    int counter4 = 0;

    counter3++;


    while (counter1 < n - counter3 && counter2 < n - counter3)
    {

        counter1++;
        counter2++;

        if (values[counter1] < values[counter2])
        {
            int array_temp[1] = {0};
            array_temp[0] = values[counter1]; 
            values[counter1] = values[counter2];
            values[counter2] = array_temp[0];
            counter4++;
        }


    }

    if (counter4 == 0)
    EXIT++;
}


return;
}
1

The only thing wrong in your searching algorithm that i'm seeing is that you have your if statement followed by a second if statement. I believe that that should be an else if.

5
  • Thanks for spotting that, but it still won't work. Anything else? – Jason_V Jun 2 '17 at 23:22
  • 1
    I believe your code is correct in execution. Testing on my computer, it works as it should. I have no idea as to why it is not working for you. – Davis Jun 2 '17 at 23:36
  • Are you certain? Although it compiles and returns, it never finds the needle for me, even if I manually enter the needle in an array. It fails check50 as well. – Jason_V Jun 3 '17 at 0:41
  • 1
    I can't tell you why it is not working but i'll give you some suggestions in your program. First of all, in your sorting program, using int array_temp[1] = {0}; is unnessesary. Instead of creating an array, you simply need to create a placeholder variable that will store one of the values. i.e. `int temp = values[counter1] I also don't know why you have {} around the 0. Final suggestion is to try to print out what your sorting algorthim says to debug it (on each iteration print the steps it takes). Perhaps you will learn something from that. I'm sorry I could not adress the problem. – Davis Jun 3 '17 at 0:55
  • Thanks for your help. My mistake was that my loops in the binary search had flipped inequality signs! I feel so stupid now. XD – Jason_V Jun 3 '17 at 5:20
0

ANSWER:

I accidentally switched the inequality operators on two of my conditions in the binary search.

else if (values[counter3] > value) 

should be

else if (values[counter3] < value)

and vice versa. A silly mistake, but a fatal one! :P

You must log in to answer this question.

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