Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Doesn't this run forever? Once length reaches 1 it will never go to 0.


Oops; you're right. One possible fix is

    for (size_t length = end - begin; length != 1; length = (length + 1) / 2)
    {
        size_t step = length / 2;
        if (compare(begin[step], value))
            begin += step;
    }   
    return begin + compare(*begin, value);
but it admittedly detracts from the simplicity of the former...




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: