Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 06 / linearSearch.cpp
diff options
context:
space:
mode:
Commitae90cb76ace17c6a2b0bee34d151242855ca5bab (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2021-11-15 16:52:00 +0200
Committer
 GitHub <noreply@github.com>
2021-11-15 16:52:00 +0200
Files5b423fb84a8487562376d04011677b9e09ea4312 /Sem. 06/linearSearch.cpp
Introduction_to_programming_FMI-ae90cb76ace17c6a2b0bee34d151242855ca5bab.tar
Introduction_to_programming_FMI-ae90cb76ace17c6a2b0bee34d151242855ca5bab.tar.gz
Introduction_to_programming_FMI-ae90cb76ace17c6a2b0bee34d151242855ca5bab.zip
Parent2a0469e65b9d4e0dde42656c199c7dd477e7e139 (diff)
Add files via upload
1 files changed, 45 insertions, 0 deletions
-rw-r--r--Sem. 06/linearSearch.cpp45
diff --git a/Sem. 06/linearSearch.cpp b/Sem. 06/linearSearch.cpp
new file mode 100644
index 0000000..d5d2ff1
--- /dev/null
+++ b/Sem. 06/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);
+}
+