Website Summary Files Commits Branches @commit @diff @stats
path: root / pract_SI / Pract. 05 / Solutions / task6.cpp
diff options
context:
space:
mode:
Commitfc2f279a9344c31232275103c485d5c6335635ea (patch)
Author
 Justsvetoslavov <justsvetoslavov@gmail.com>
2021-11-16 16:56:56 +0200
Committer
 Justsvetoslavov <justsvetoslavov@gmail.com>
2021-11-16 16:56:56 +0200
Files43db693d0a6147cb4964431a0a752927029a729e /pract_SI/Pract. 05/Solutions/task6.cpp
Introduction_to_programming_FMI-fc2f279a9344c31232275103c485d5c6335635ea.tar
Introduction_to_programming_FMI-fc2f279a9344c31232275103c485d5c6335635ea.tar.gz
Introduction_to_programming_FMI-fc2f279a9344c31232275103c485d5c6335635ea.zip
Parenta4ecc80098b95dfc06fccaec0bf8ebed6c6f538e (diff)
Added solutions Pract.05
1 files changed, 26 insertions, 0 deletions
-rw-r--r--pract_SI/Pract. 05/Solutions/task6.cpp26
diff --git a/pract_SI/Pract. 05/Solutions/task6.cpp b/pract_SI/Pract. 05/Solutions/task6.cpp
new file mode 100644
index 0000000..a3804fc
--- /dev/null
+++ b/pract_SI/Pract. 05/Solutions/task6.cpp
@@ -0,0 +1,26 @@
+/*
+A iterative binary search function.
+It returns location of x in given array arr[l..r] if present,
+otherwise -1.
+*/
+
+//int l - left side index
+//int r - right side index
+//int m - middle index
+//int x - number 'x' we are searching for
+
+int binarySearch1(int *arr, int l, int r, int x)
+{
+ while (l <= r)
+ {
+ int m = l + (r - l) / 2;
+ if (arr[m] == x)// Check if x is present at mid
+ return m;
+ if (arr[m] < x)// If x greater, ignore left half
+ l = m + 1;
+ else
+ r = m - 1; // If x is smaller, ignore right half
+ }
+
+ return -1; // if we reach here, then the element was not present
+} \ No newline at end of file