
- 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
What is the best way to search for an item in a sorted list in JavaScript?
As far as sorted arrays are concerned (be it in any order), binary search is the most optimized and efficient search algorithm to exist. We are required to write a binary search function that searches a sorted array of literals for a target.
We should then attach that function to the prototype property of Array Objects.
Example
The code for this will be −
const arr = [2, 5, 8, 12, 14, 16, 17, 22, 26, 28, 35, 67, 78, 99]; const target = 22; Array.prototype.binarySearch = function(target) { if ( !this.length ) { return false; } if ( this[0] === target ) { return true; } var i, mid, start = 0, end = this.length, c = false; while ( c = (i = this[mid = start+((end-start)>>1)]) !== target ) { i < target ? (start = mid) : (end = mid); if (start >= end - 1) { break; } } return !c; }; console.log(arr.binarySearch(target));
Output
And the output in the console will be −
true
- Related Articles
- How to search for an item in a Lua List?
- What is the best way to remove an item from a Python dictionary?
- What is the best way to add an event in JavaScript?
- What is the best way to concatenate strings in JavaScript?
- What is the best way to initialize a JavaScript number?
- What is best way to check if a list is empty in Python?
- What is the best way to handle list empty exception in Python?
- What is the best way to compare two strings in JavaScript?
- What is the best way to convert a number to a string in JavaScript?
- What is the best way to initialize a JavaScript Date to midnight?
- What is the best way to do optional function parameters in JavaScript?
- What is the best way to break from nested loops in JavaScript?
- What is the best way of declaring multiple Variables in JavaScript?
- Python - Inserting item in sorted list maintaining order
- What is the best way to handle a Javascript popup using Selenium Webdriver?

Advertisements