Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8.  
  9. int main() {
  10.     int searchVal; int numValues; int input;
  11.     vector<int> listVal;
  12.     cin >> searchVal >> numValues;
  13.     for (int i = 0; i < numValues; i++) {
  14.         cin >> input;
  15.         listVal.push_back(input);
  16.     }
  17.     int pivot = numValues / 2;
  18.     bool isFound = false;
  19.     int startPos = 0; int endPos = numValues - 1;
  20.     while (pivot >0 && !isFound) {
  21.         int pivot = (endPos - startPos) / 2;
  22.         if (listVal[pivot] == searchVal) {
  23.             isFound = true;
  24.             cout << pivot << endl;
  25.         }
  26.         else if (listVal[pivot] > searchVal) {
  27.             endPos = pivot;
  28.         }
  29.        
  30.         else {
  31.             startPos = pivot;
  32.         }
  33.     }
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement