looks fairly simple to me. note this works of any ordering of R and L if the data type is signed.
looks fairly simple to me. note this works of any ordering of R and L if the data type is signed.
However, the linked-to binary search starts from 0:
1: public static int binarySearch(int[] a, int key) {
2: int low = 0;
3: int high = a.length - 1;
and the promoted fix is: 6: int mid = low + ((high - low) / 2);
The aforementioned Wikipedia binary sort also takes a length, rather than start/end: function binary_search(A, n, T) is
L := 0
R := n − 1The lower_bound you pointed to takes start/end iterators defining the partially-ordered range to examine.
The linked-to essay and the Wikipedia take array + size.
These are different APIs.
The latter - which is what this thread is about, IMO - is easier to implement because the sizes are always non-negative.
https://en.cppreference.com/w/cpp/named_req/RandomAccessIter...
they were conceived for c++'98 and transmogrified to a trait for c++20
the run time in GPs lower_bound link is log(n) for random access iterators and liner for non-random access.
But how does that change my observation about the differences in the two APIs?