From 745733dd452b7632a1968b97603b83a868b71854 Mon Sep 17 00:00:00 2001 From: Angel Dimitriev Date: Wed, 22 Dec 2021 19:06:42 +0200 Subject: Rename binarySearch.cpp to BinarySearch.cpp --- Sem. 11/BinarySearch.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ Sem. 11/binarySearch.cpp | 44 -------------------------------------------- 2 files changed, 44 insertions(+), 44 deletions(-) create mode 100644 Sem. 11/BinarySearch.cpp delete mode 100644 Sem. 11/binarySearch.cpp diff --git a/Sem. 11/BinarySearch.cpp b/Sem. 11/BinarySearch.cpp new file mode 100644 index 0000000..df678ce --- /dev/null +++ b/Sem. 11/BinarySearch.cpp @@ -0,0 +1,44 @@ +#include +#include +using namespace std; + + +bool linearSearch(const int arr[], unsigned size, int el) +{ + for (unsigned i = 0; i < size; i++) + { + if (arr[i] == el) + return true; + } + return false; +} + +bool binarySearch(const int* arr, unsigned size, int searched) +{ + unsigned fromIndex = 0; + unsigned toIndex = size - 1; + + while (fromIndex <= toIndex) + { + unsigned midIndex = fromIndex + (toIndex - fromIndex) / 2; + + if (arr[midIndex] == searched) + return true; + + if (arr[midIndex] < searched) + fromIndex = midIndex + 1; + else + toIndex = midIndex - 1; + } + + return false; +} + + + +int main() +{ + const unsigned SIZE = 8; + int arr[] { 1, 12, 33, 14,35,4,17,38}; + cout << linearSearch(arr, SIZE, 14); +} diff --git a/Sem. 11/binarySearch.cpp b/Sem. 11/binarySearch.cpp deleted file mode 100644 index df678ce..0000000 --- a/Sem. 11/binarySearch.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include -#include -using namespace std; - - -bool linearSearch(const int arr[], unsigned size, int el) -{ - for (unsigned i = 0; i < size; i++) - { - if (arr[i] == el) - return true; - } - return false; -} - -bool binarySearch(const int* arr, unsigned size, int searched) -{ - unsigned fromIndex = 0; - unsigned toIndex = size - 1; - - while (fromIndex <= toIndex) - { - unsigned midIndex = fromIndex + (toIndex - fromIndex) / 2; - - if (arr[midIndex] == searched) - return true; - - if (arr[midIndex] < searched) - fromIndex = midIndex + 1; - else - toIndex = midIndex - 1; - } - - return false; -} - - - -int main() -{ - const unsigned SIZE = 8; - int arr[] { 1, 12, 33, 14,35,4,17,38}; - cout << linearSearch(arr, SIZE, 14); -} -- cgit v1.3.1-23-gd82d