Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. template<typename It>
  2. It find_minimum(It begin, It end) {
  3.     if(begin == end) return end;
  4.  
  5.     auto l = begin;
  6.     auto r = std::prev(end);
  7.  
  8.     while(l != r) {
  9.         auto m = l;
  10.         auto middle = std::distance(l, r) / 2;
  11.         std::advance(m, middle);
  12.         auto n = std::next(m);
  13.         if(*m < *n) {
  14.             r = m;
  15.         } else {
  16.             l = n;
  17.         }
  18.     }
  19.     return l;
  20. }
  21.  
  22.  
  23. int main(int argc, char** argv) {
  24.     std::vector<T> arr = { 4, 3, 2, 1, 5, 6, 7 };
  25.     auto res = find_minimum(arr.begin(), arr.end(), 1);
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement