I don't like the index variables and splitting in half part. I like to write my binary searches with bit patterns: 1) figure out the number of bits required to cover the largest index (= a little bit of cheap bit-twiddling) 3) initialize your index to 0 2) flip a bit on in index, starting from the highest bit available from step 1) 3) if data[index] is smaller than what you're looking for, leave the bit on 4) try wit…
int binary_search_pow2(const int a[], int len, int key) {
int i = 0, step;
for (step = len / 2; step > 0; step >>= 1)
if (a[i | step]
I thought that only worked for power-of-two sized arrays.