Open In App

JavaScript Program for Binary Search using Recursion

Last Updated : 20 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will demonstrate the Binary Search in JavaScript using the Recursive approach.

Binary Search is a searching technique that works on the Divide and Conquer approach. It is faster than the linear search. It only works for the sorted arrays. It takes in O(log n) time for execution.

Approach

  • Create a function named recursiveBS that accepts 4 arguments i.e., array, target element, left index, and right index.
  • If the left index is greater than the right it means the element does not exist hence return -1.
  • Compute the mid index and check if the array at mid value is equal to the target then return the index.
  • Else if the target value is less than the mid element then call the function for the left half.
  • Else if the target value is greater than the mid element then call the function.
  • Call the recursiveBS for the given array and target element.
  • if the function returns -1 that means the element does not exist and print the output.
  • else output the index returned by the recursiveBS function using the console.log method.

Example: In this example, we will see the implementation of binary search using recursion.

Javascript




// Recursive function for binary search
function recursiveBS(arr, target, left, right) {
    // -1 for element not found
    if (left > right) return -1;
  
    // Get the mid index
    mid = Math.floor((left + right) / 2);
  
    // If target found return index
    if (arr[mid] === target) return mid;
    // If tagert is less than mid index value
    // search again in left half
    else if (arr[mid] > target) {
        return recursiveBS(arr, target, left, mid - 1);
    }
    // If target is greater than mid index value
    // search in right half
    else {
        return recursiveBS(arr, target, mid + 1, right);
    }
    return -1;
}
  
arr = [1, 2, 6, 7, 11, 13, 15, 18];
target = 7;
index = recursiveBS(arr, target, 0, arr.length - 1);
  
if (index == -1)
    console.log(
        target + " is not present in the given array"
    );
else 
    console.log(target + " is present at index: " + index);


Output

7 is present at index: 3

Similar Reads

JavaScript Program to Convert Decimal to Binary Using Recursion
JavaScript allows us to convert decimal numbers to their binary equivalents using recursion, offering two distinct approaches. Through recursive division and concatenation, the first method constructs the binary representation directly. The second method leverages a string and recursion, providing an alternative approach. Each method's time and spa
2 min read
JavaScript Program to Find Kth Element of Two Sorted Arrays using Binary Search
Given two sorted arrays, our task is to find the Kth element in the combined array made by merging the two input arrays using Binary Search in JavaScript. Example: Input: Arr1: [1, 2, 5] , Arr2:[2, 4, 6, 8], K = 4Output: Kth element is 4.Explanation:The final Array would be: [1, 2, 2, 4, 5, 6, 8]The 4th element of this array is 4.ApproachInitialize
2 min read
JavaScript Program to Find Sum of Natural Numbers using Recursion
We are given a number N and the task is to find the sum of the first n natural numbers using recursion. Approach to Find Sum of Natural Numbers using Recursion:In this approach, we are using the function to get the sum of the first N natural numbers. First, we declare a function findSum(n) and pass the number (n) till now sum is calculated. Then ch
2 min read
JavaScript Program to Find Factorial of a Number using Recursion
In this article, we are going to learn about finding a Factorial of a Number using Recursion. Calculating the factorial of a number using recursion means implementing a function that calls itself to find the factorial of a given number. The factorial of a non-negative integer "n" is the product of all positive integers less than or equal to "n". Th
2 min read
JavaScript Program to Check for Palindrome String using Recursion
Given a string, write a recursive function that checks if the given string is a palindrome, else, not a palindrome. A string is called a palindrome if the reverse of the string is the same as the original one. For example - “madam”, “racecar”, etc. What is Recursion?The process in which a function calls itself directly or indirectly is called recur
2 min read
JavaScript Program to Print 1 to N using Recursion
In this article, we will see how to print 1 to N using Recursion in JavaScript. What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. In the recursive program, the solution to the base case is provided and the solution to the bigger p
2 min read
JavaScript Program to Print N to 1 using Recursion
In this article, we will see how to print N to 1 using Recursion in JavaScript. What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. In the recursive program, the solution to the base case is provided and the solution to the bigger p
1 min read
JavaScript Program to Display Fibonacci Sequence Using Recursion
In this article, we will explore how to display the Fibonacci sequence using recursion in JavaScript. The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. Recursion is a powerful technique in programming that involves a function calling itself. A recursive function refers to a function that calls its
3 min read
JavaScript Program for Solving f(n)= (1) + (2*3) + (4*5*6) … n using Recursion
In this article, we will learn how we can solve the sum of a series that follows a specific pattern. In this problem, we will explore the process of determining the sum of the series mentioned below. Upon examination of the pattern presented it becomes evident that each term, in the series is obtained by multiplying numbers together. Example: Input
2 min read
JavaScript Program to Find the Sum of Natural Numbers using Recursion
Finding the sum of natural numbers using recursion involves defining a function that recursively adds numbers from 1 to the given limit. The function repeatedly calls itself with decreasing values until reaching the base case, where it returns the sum. Example: Input: 5Output: 15Explanation: 1 + 2 + 3 + 4 + 5 = 15Input: 10Output: 55Explanation: 1 +
1 min read