From e5acafc6121230d85a90a5e7c3568195f334a807 Mon Sep 17 00:00:00 2001 From: Raghavendra naik Date: Wed, 24 Oct 2018 13:49:59 +0530 Subject: [PATCH] Update Problem-Solving/binary_search.js --- Problem-Solving/binary_search.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Problem-Solving/binary_search.js diff --git a/Problem-Solving/binary_search.js b/Problem-Solving/binary_search.js new file mode 100644 index 0000000..a82ffd2 --- /dev/null +++ b/Problem-Solving/binary_search.js @@ -0,0 +1,28 @@ +let recursiveFunction = function (arr, x, start, end) { + + if (start > end) return false; + + let mid=Math.floor((start + end)/2); + + if (arr[mid]===x) return true; + + if(arr[mid] > x) + return recursiveFunction(arr, x, start, mid-1); + else + return recursiveFunction(arr, x, mid+1, end); +} + +// Driver code +let arr = [1, 3, 5, 7, 8, 11]; +let x = 5; + +if (recursiveFunction(arr, x, 0, arr.length-1)) + console.log("Element found.."); +else console.log("Element not found..."); + +x = 6; + +if (recursiveFunction(arr, x, 0, arr.length-1)) + console.log("Element found"); +else + console.log("Element not found"); \ No newline at end of file