0

I was looking through the list of algorithms and decided to have a look at the find method

Find Method

template<class InputIterator, class T>
  InputIterator find (InputIterator first, InputIterator last, const T& val)
{
  while (first!=last) {
    if (*first==val) return first;
    ++first;
  }
  return last;
}

It seems Find has a running time of Big O (n) as it iterates through every element in the container to find the value.

My thoughts immediately thought of Binary Search and I went to have a look at it .

Binary Search

template <class ForwardIterator, class T>
  bool binary_search (ForwardIterator first, ForwardIterator last, const T& val)
{
  first = std::lower_bound(first,last,val);
  return (first!=last && !(val<*first));
}

Knowing that it utilises another function : lower bound , I went on to research lower bound which returns an iterator pointing to the first element in the range [first,last) which does not compare less than val.

This is my analysis

Assuming an array data structure

[ 1 , 4 , 7 , 10 , 12 ]

If we use binary search to search for 6 , first would be equal to an iterator pointing to 7

*first would be of value 1 , val would be of value 6 then !(val<*first) would be true

Since first!=last == true && !(val<*first) == true , the binary search function would return true even though 6 does not exist in the array

I know there is flaw in my reasoning , can someone explain to me where did i go wrong ???

4
  • 2
    If first is an iterator pointing to 7, then it seems unlikely that *first would be of value 1... Commented Feb 9, 2014 at 10:26
  • first would be equal to an iterator pointing to 7 and *first would be of value are incompatible statements. Pick one you think is true and continue from there. Commented Feb 9, 2014 at 10:29
  • assuming I looked for 7 instead of 6 , *first would be 10 hence !(val<*first) would be false and binary search would return false though 7 exist in the array , whats wrong with this reasoning Commented Feb 9, 2014 at 10:44
  • No, if you looked for 7 *first would be 7, because 7 is the first element not less than 7. Commented Feb 9, 2014 at 11:02

1 Answer 1

3

*first would be of value 1

There's your problem. first is an iterator to the element with value 7, so *first is 7. That makes !(val<*first) become !(6<7) which is false.

Sign up to request clarification or add additional context in comments.

3 Comments

assuming I looked for 7 instead of 6 , *first would be 10 hence !(val<*first) would be false and binary search would return false though 7 exist in the array , whats wrong with this reasoning – Computernerd 8 mins ago
@Computernerd That's not true. lower_bound finds the first value that is not lower than the value you're looking for. So it would find 7.
The issue is that to actually find the value (as opposed to bounding it), the function would have to take a less-than comparison operator and an equality-comparison operator. The design philosophy of the STL is genericity and performance. There is no performance cost to using lower_bound and then testing for equality in your own function. Some things are not equality comparable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.