Skip to main content
1 of 4
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341

##Interface

Passing an array and then two indexes is a very C style of interface. In C++ it is much more common to pass two iterators. An iterator is a generalized form of pointer.

So rather than:

bool binary_search(int ar[], int low, int high, int key);

I would use:

template<typename I>
bool binary_search(I low, I high, int key);

Also rather than pointing at the first and last elements iterator ranges use a first and one past the last. This makes calculating sizes easier.

###Code Review

Don't use >> 1 to represent a divide by 2. The point of high level code is to write it so that it is easy for humans to read. That is not obvious. Also the compiler can do these micro optimizations much better than you. So don't try and confuse it. Just write code in the most readable way possible.

Loki Astari
  • 97.7k
  • 5
  • 126
  • 341