Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 11 / BinarySearch.cpp
diff options
context:
space:
mode:
Commit745733dd452b7632a1968b97603b83a868b71854 (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2021-12-22 19:06:42 +0200
Committer
 GitHub <noreply@github.com>
2021-12-22 19:06:42 +0200
Files9958d286da0b314ac29510b82d9b088d22d3419f /Sem. 11/BinarySearch.cpp
Introduction_to_programming_FMI-745733dd452b7632a1968b97603b83a868b71854.tar
Introduction_to_programming_FMI-745733dd452b7632a1968b97603b83a868b71854.tar.gz
Introduction_to_programming_FMI-745733dd452b7632a1968b97603b83a868b71854.zip
Parent9002b71f11507389e83248517a10e7851d44f075 (diff)
Rename binarySearch.cpp to BinarySearch.cpp
1 files changed, 44 insertions, 0 deletions
-rw-r--r--Sem. 11/BinarySearch.cpp44
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 <iostream>
+#include <vector>
+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);
+}