Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 11 / LinearSearch.cpp
diff options
context:
space:
mode:
Commit4fb718f5fc6088641ecad2aeccd321b77b8bd356 (patch)
Author
 Justsvetoslavov <justsvetoslavov@gmail.com>
2021-12-26 02:42:24 +0200
Committer
 Justsvetoslavov <justsvetoslavov@gmail.com>
2021-12-26 02:42:24 +0200
Files7f0abd8e37eed78ccc0b45a65de80450d1c59eef /Sem. 11/LinearSearch.cpp
Introduction_to_programming_FMI-4fb718f5fc6088641ecad2aeccd321b77b8bd356.tar
Introduction_to_programming_FMI-4fb718f5fc6088641ecad2aeccd321b77b8bd356.tar.gz
Introduction_to_programming_FMI-4fb718f5fc6088641ecad2aeccd321b77b8bd356.zip
Parent158370669b24142a71ce7778b46f5a054cafed8a (diff)
Parentb3e29bba94da067984c841d7904887a4bd5995ea (diff)
Merge branch 'main' of https://github.com/Angeld55/Introduction_to_programming_FMI
1 files changed, 45 insertions, 0 deletions
-rw-r--r--Sem. 11/LinearSearch.cpp45
diff --git a/Sem. 11/LinearSearch.cpp b/Sem. 11/LinearSearch.cpp
new file mode 100644
index 0000000..b0446e1
--- /dev/null
+++ b/Sem. 11/LinearSearch.cpp
@@ -0,0 +1,45 @@
+#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);
+}
+