0

my while loop won't stop even when the value of min is greater than the value of max and I can't figure out why

int min = 0;
int max = n - 1;
int midpoint = max / 2;

while (min != max || min < max)
{
    if (values[min] == value || values[max] == value)
    {
        return true;
        break;
    }
    else if (values[midpoint] > value)
    {
        max = midpoint - 1;
        midpoint = min + ((max - min) / 2);
    }
    else if (values[midpoint] < value)
    {
        min = midpoint + 1;
        midpoint = max - ((max - min) / 2);
    }
    else
    {
        return true;
    }
}
return false;

1 Answer 1

1

Consider: if min > max, will the first condition be met or not?

You must log in to answer this question.

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