
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Implementing binary search in JavaScript to return the index if the searched number exist
We are required to write a JavaScript function that takes in a sorted array of numbers as the first argument and a search number as the second argument.
If the search number exists in the array, we need to return its index in the array, otherwise we need to return -1.
We have to do this making use of the binary search algorithm. The binary search algorithm is basically a divide and conquer algorithm which recursive divides the array into halves until it converses to a singleton element.
The sorting of array is necessary of binary search algorithm in this case, as it makes deciding which part to divide easy for us.
Example
const arr = [-3, -1, 4, 7, 9, 11, 14, 22, 26, 28, 36, 45, 67, 78, 88, 99]; const binarySearch = (arr = [], num) => { let l = 0; let r = arr.length - 1; while(l <= r){ const mid = Math.floor((l + r) / 2); if(num == arr[mid]){ return mid; } else if(num < arr[mid]){ r = mid - 1; } else{ l = mid + 1; }; }; return -1 }; console.log(binarySearch(arr, 22)); console.log(binarySearch(arr, 56)); console.log(binarySearch(arr, 11));
Output
And the output in the console will be −
7 -1 5
- Related Articles
- Implementing a Binary Search Tree in JavaScript
- Implementing Linear Search in JavaScript
- Implementing block search in JavaScript
- Binary Search Tree in Javascript
- Binary Search program in JavaScript
- Implementing incremental search and display the values with a specific number in MySQL?
- Implementing Math function and return m^n in JavaScript
- Binary Search Tree Class in Javascript
- Search in an array with Binary search using JavaScript
- Can Search Engines Index JavaScript?
- Count the Number of Binary Search Trees present in a Binary Tree in C++
- Python Pandas - Return the number of bytes in the underlying Index data
- Python Pandas - Return the Number of elements in the underlying Index data
- Python Pandas - Return number of unique elements in the Index object
- Implementing the Array.prototype.lastIndexOf() function in JavaScript

Advertisements