Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar01 / src / searching-algorithms / binary-search.cpp
diff options
context:
space:
mode:
Commitc9e3d1dd8c76da82db677570bbbd555405686a78 (patch)
Author
 Stoycho Kyosev <skyosev@192.168.100.17>
2024-10-11 02:24:47 -0400
Committer
 Stoycho Kyosev <skyosev@192.168.100.17>
2024-10-11 02:24:47 -0400
Filesc266de9020071ef2400f13063a02cb7514407b0a /Seminar01/src/searching-algorithms/binary-search.cpp
Data-structures-and-algorithms-c9e3d1dd8c76da82db677570bbbd555405686a78.tar
Data-structures-and-algorithms-c9e3d1dd8c76da82db677570bbbd555405686a78.tar.gz
Data-structures-and-algorithms-c9e3d1dd8c76da82db677570bbbd555405686a78.zip
Parentddf71c2b4804b7a86aa9bd48e385603b7623f244 (diff)
Add seminar01
1 files changed, 33 insertions, 0 deletions
-rw-r--r--Seminar01/src/searching-algorithms/binary-search.cpp33
diff --git a/Seminar01/src/searching-algorithms/binary-search.cpp b/Seminar01/src/searching-algorithms/binary-search.cpp
new file mode 100644
index 0000000..b5befb0
--- /dev/null
+++ b/Seminar01/src/searching-algorithms/binary-search.cpp
@@ -0,0 +1,33 @@
+#include <iostream>
+
+template <class T>
+int binarySearch(const T* arr, size_t size, T elem)
+{
+ int left = 0;
+ int right = size - 1;
+
+ while (right - left >= 0)
+ {
+ int mid = left + (right - left) / 2;
+
+ if(arr[mid] == elem)
+ {
+ return mid;
+ }
+ else if (arr[mid] > elem)
+ {
+ right = mid - 1;
+ }
+ else
+ {
+ left = mid + 1;
+ }
+ }
+
+ return -1;
+}
+
+int main()
+{
+
+} \ No newline at end of file