Update MP2

This commit is contained in:
2024-03-11 08:50:33 -05:00
parent 53268a2dc0
commit debcca3729
69 changed files with 4175 additions and 7723 deletions

View File

@@ -11,12 +11,13 @@ int binarySearch(std::vector<int> arr, int numToSearchFor) {
int first = 0;
int last = arr.size() - 1;
int position = -1;
bool found = false;
while (first <= last) {
while (!found && first <= last) {
int middle = first + (last - first) / 2;
if (arr[middle] == numToSearchFor) {
found = true;
position = middle;
break;
}
else if (arr[middle] > numToSearchFor) {
last = middle - 1;
@@ -24,7 +25,11 @@ int binarySearch(std::vector<int> arr, int numToSearchFor) {
first = middle + 1;
}
}
return position;
if (!found) {
return -1;
} else {
return position;
}
}
std::tuple<int, int> binarySearch(std::vector<std::vector<int>> arr, int numToSearchFor) {
@@ -36,3 +41,22 @@ std::tuple<int, int> binarySearch(std::vector<std::vector<int>> arr, int numToSe
}
return {-1, -1};
}
int linearSearch(std::vector<int> arr, int numToSearchFor) {
for (int i = 0; i < arr.size(); ++i) {
if (arr[i] == numToSearchFor) {
return i;
}
}
return -1;
}
std::tuple<int, int> linearSearch(std::vector<std::vector<int>> arr, int numToSearchFor) {
for (int i = 0; i < arr.size(); ++i) {
int columnLocation = linearSearch(arr[i], numToSearchFor);
if (columnLocation > -1) {
return {i, columnLocation};
}
}
return {-1, -1};
}