K&R Exercises

Exercise 3-1: Binary search with one test in the loop

Our binary search makes two tests inside the loop, when one would suffice (at the price of more tests outside.) Write a version with only one test inside the loop and measure the difference in run-time.

/* binsearch:  find x in v[0] <= v[1] <= ... <= v[n-1] */
int binsearch(int x, int v[], int n)
{
    int low, high, mid;

    low = 0;
    high = n - 1;
    while (low <= high) {
        mid = (low+high)/2;
        if (x < v[mid])
            high = mid - 1;
        else if (x > v[mid])
            low = mid + 1;
        else    /* found match */
            return mid;
    }
    return -1;   /* no match */
}

The first input line holds n, at most 1000. The next n lines hold v[0] to v[n-1], in strictly increasing order, and each line after that holds a value of x. For each x, print binsearch(x, v, n) on a line of its own.

Test Cases

· space→ tab↵ newline

Submissions

No submissions yet