-
Notifications
You must be signed in to change notification settings - Fork 12.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f1c1f6
commit c13fc26
Showing
1 changed file
with
20 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,46 @@ | ||
--- | ||
title: Binary search | ||
type: snippet | ||
title: Binary search in a sorted JavaScript array | ||
shortTitle: Binary search | ||
type: tip | ||
language: javascript | ||
tags: [algorithm,array] | ||
cover: zen-indoors | ||
excerpt: Finds the index of a given element in a sorted array using the binary search algorithm. | ||
excerpt: Use the binary search algorithm to find the index of a given element in a sorted array. | ||
listed: true | ||
dateModified: 2020-12-29 | ||
dateModified: 2024-07-20 | ||
--- | ||
|
||
Finds the index of a given element in a sorted array using the binary search algorithm. | ||
The [binary search algorithm](https://en.wikipedia.org/wiki/Binary_search) is a fast and efficient way to **find the index of a given element in a sorted array**. It works by repeatedly dividing the search interval in half, narrowing down the possible locations of the element. | ||
|
||
- Declare the left and right search boundaries, `l` and `r`, initialized to `0` and the `length` of the array respectively. | ||
- Use a `while` loop to repeatedly narrow down the search subarray, using `Math.floor()` to cut it in half. | ||
- Return the index of the element if found, otherwise return `-1`. | ||
A binary search is much faster than a [linear search](/js/s/linear-search), especially for large arrays, as it has a **time complexity** of `O(log n)`. However, it requires the array to be sorted beforehand. | ||
|
||
> [!NOTE] | ||
> | ||
> Does not account for duplicate values in the array. | ||
In order to implement the algorithm, we need to keep track of the left and right **boundaries** of the search interval, and repeatedly divide it in half until the element is found or the interval is empty. The boundaries are initialized to `0` and the length of the array, respectively. | ||
|
||
Then, using a `while` **loop**, we calculate the **middle index** of the current interval and compare the element at that index with the target element. If the element is **found**, we return the index. Otherwise, we update the boundaries based on the comparison and continue the search. | ||
|
||
If the element is **not found** after the loop, we return `-1` to indicate that the element is not present in the array. | ||
|
||
```js | ||
const binarySearch = (arr, item) => { | ||
let l = 0, | ||
r = arr.length - 1; | ||
let l = 0, r = arr.length - 1; | ||
|
||
while (l <= r) { | ||
const mid = Math.floor((l + r) / 2); | ||
const guess = arr[mid]; | ||
|
||
if (guess === item) return mid; | ||
if (guess > item) r = mid - 1; | ||
else l = mid + 1; | ||
} | ||
|
||
return -1; | ||
}; | ||
|
||
binarySearch([1, 2, 3, 4, 5], 1); // 0 | ||
binarySearch([1, 2, 3, 4, 5], 5); // 4 | ||
binarySearch([1, 2, 3, 4, 5], 6); // -1 | ||
``` | ||
|
||
> [!NOTE] | ||
> | ||
> This implementation **does not account for duplicate values** in the array. |