diff options
Add seminar01
1 files changed, 33 insertions, 0 deletions
| -rw-r--r-- | Seminar01/src/searching-algorithms/binary-search.cpp | 33 |
diff --git a/Seminar01/src/searching-algorithms/binary-search.cpp b/Seminar01/src/searching-algorithms/binary-search.cpp new file mode 100644 index 0000000..b5befb0 --- /dev/null +++ b/Seminar01/src/searching-algorithms/binary-search.cpp @@ -0,0 +1,33 @@ +#include <iostream> + +template <class T> +int binarySearch(const T* arr, size_t size, T elem) +{ + int left = 0; + int right = size - 1; + + while (right - left >= 0) + { + int mid = left + (right - left) / 2; + + if(arr[mid] == elem) + { + return mid; + } + else if (arr[mid] > elem) + { + right = mid - 1; + } + else + { + left = mid + 1; + } + } + + return -1; +} + +int main() +{ + +}
\ No newline at end of file |
