From cdb7437f119be7609ca0fab603d52c2cff3bc464 Mon Sep 17 00:00:00 2001
From: prajwal3006 <147615277+prajwal3006@users.noreply.github.com>
Date: Sat, 14 Oct 2023 15:11:01 +0530
Subject: [PATCH] Create binary search.js
---
binary search.js | 37 +++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)
create mode 100644 binary search.js
diff --git a/binary search.js b/binary search.js
new file mode 100644
index 0000000..7fb629d
--- /dev/null
+++ b/binary search.js
@@ -0,0 +1,37 @@
+// Iterative function to implement Binary Search
+let iterativeFunction = function (arr, x) {
+
+ let start=0, end=arr.length-1;
+
+ // Iterate while start not meets end
+ while (start<=end){
+
+ // Find the mid index
+ let mid=Math.floor((start + end)/2);
+
+ // If element is present at mid, return True
+ if (arr[mid]===x) return true;
+
+ // Else look in left or right half accordingly
+ else if (arr[mid] < x)
+ start = mid + 1;
+ else
+ end = mid - 1;
+ }
+
+ return false;
+}
+
+// Driver code
+let arr = [1, 3, 5, 7, 8, 9];
+let x = 5;
+
+if (iterativeFunction(arr, x, 0, arr.length-1))
+ document.write("Element found!
");
+else document.write("Element not found!
");
+
+x = 6;
+
+if (iterativeFunction(arr, x, 0, arr.length-1))
+ document.write("Element found!
");
+else document.write("Element not found!
");