From dd9a7bf0fdcc3a04fcfb18ba1613d91b46ef0833 Mon Sep 17 00:00:00 2001 From: Stoycho Kyosev Date: Sun, 20 Oct 2024 07:47:14 -0400 Subject: Add Sem02 --- Seminar01/src/searching-algorithms/binary-search.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'Seminar01/src/searching-algorithms/binary-search.cpp') diff --git a/Seminar01/src/searching-algorithms/binary-search.cpp b/Seminar01/src/searching-algorithms/binary-search.cpp index b5befb0..7404e0a 100644 --- a/Seminar01/src/searching-algorithms/binary-search.cpp +++ b/Seminar01/src/searching-algorithms/binary-search.cpp @@ -27,7 +27,21 @@ int binarySearch(const T* arr, size_t size, T elem) return -1; } +constexpr unsigned EXAMPLE_SIZE = 7; + int main() { - + int example[EXAMPLE_SIZE] = { 1, 2, 4, 8, 16, 32, 64 }; + int needle = 4; + + int index = binarySearch(example, EXAMPLE_SIZE, needle); + if(index == -1) + { + std::cout << "Unable to find the element" << std::endl; + } + else + { + std::cout << "Element " << needle << " found at " + << index << " position: " << example[index] << std::endl; + } } \ No newline at end of file -- cgit v1.3.1-23-gd82d