How to apply binary search O(log n) on a sorted linked list?

It is certainly not possible with a plain singly-linked list. Sketch proof: to examine the last node of a singly-linked list, we must perform n-1 operations of following a “next” pointer [proof by induction on the fact that there is only one reference to the k+1th node, and it is in the kth node, and … Read more

Where can I get a “useful” C++ binary search algorithm?

There is no such functions, but you can write a simple one using std::lower_bound, std::upper_bound or std::equal_range. A simple implementation could be template<class Iter, class T> Iter binary_find(Iter begin, Iter end, T val) { // Finds the lower bound in at most log(last – first) + 1 comparisons Iter i = std::lower_bound(begin, end, val); if … Read more

tech