Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- int main() {
- int searchVal; int numValues; int input;
- vector<int> listVal;
- cin >> searchVal >> numValues;
- for (int i = 0; i < numValues; i++) {
- cin >> input;
- listVal.push_back(input);
- }
- int pivot = numValues / 2;
- bool isFound = false;
- int startPos = 0; int endPos = numValues - 1;
- while (pivot >0 && !isFound) {
- int pivot = (endPos - startPos) / 2;
- if (listVal[pivot] == searchVal) {
- isFound = true;
- cout << pivot << endl;
- }
- else if (listVal[pivot] > searchVal) {
- endPos = pivot;
- }
- else {
- startPos = pivot;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement