2024-03-07 13:27:19 -06:00
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
#include <climits>
|
2024-03-07 18:40:11 -06:00
|
|
|
#include <algorithm>
|
2024-03-07 13:27:19 -06:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2024-03-07 18:40:11 -06:00
|
|
|
int binarySearch(std::vector<int> arr, int numToSearchFor) {
|
|
|
|
std::sort(arr.begin(), arr.end());
|
2024-03-07 13:27:19 -06:00
|
|
|
int first = 0;
|
|
|
|
int last = arr.size() - 1;
|
|
|
|
int position = INT_MIN;
|
2024-03-07 18:40:11 -06:00
|
|
|
int middle = INT_MIN;
|
2024-03-07 13:27:19 -06:00
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
while (!found && first <= last) {
|
|
|
|
middle = (first + last) / 2;
|
|
|
|
if (arr[middle] == numToSearchFor) {
|
|
|
|
found = true;
|
|
|
|
position = middle;
|
|
|
|
}
|
2024-03-07 18:40:11 -06:00
|
|
|
else if (arr[middle] > numToSearchFor) {
|
|
|
|
last = middle - 1;
|
|
|
|
} else {
|
|
|
|
first = middle + 1;
|
|
|
|
}
|
2024-03-07 13:27:19 -06:00
|
|
|
}
|
2024-03-07 18:40:11 -06:00
|
|
|
return position;
|
2024-03-07 13:27:19 -06:00
|
|
|
}
|
2024-03-07 18:40:11 -06:00
|
|
|
|
|
|
|
std::tuple<int, int> binarySearch(std::vector<std::vector<int>> arr, int numToSearchFor) {
|
|
|
|
for (int i = 0; i < arr.size(); ++i) {
|
|
|
|
int columnLocation = binarySearch(arr[i], numToSearchFor);
|
|
|
|
if (columnLocation != INT_MIN) {
|
|
|
|
return {i, columnLocation};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {INT_MIN, INT_MIN};
|
|
|
|
}
|