Snippet format update
To match the starter (for the migration)
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
### binarySearch
|
||||
---
|
||||
title: binarySearch
|
||||
tags: algorithm,beginner
|
||||
---
|
||||
|
||||
Use recursion. Similar to `Array.prototype.indexOf()` that finds the index of a value within an array.
|
||||
The difference being this operation only works with sorted arrays which offers a major performance boost due to it's logarithmic nature when compared to a linear search or `Array.prototype.indexOf()`.
|
||||
@@ -21,4 +24,4 @@ const binarySearch = (arr, val, start = 0, end = arr.length - 1) => {
|
||||
```js
|
||||
binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 6); // 2
|
||||
binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 21); // -1
|
||||
```
|
||||
```
|
||||
Reference in New Issue
Block a user