Skip to content

Commit fafce27

Browse files
committed
Add binary search.
1 parent c7110be commit fafce27

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
* Graph
3131
* [Depth-First Search (DFS)](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/graph/depth-first-search)
3232
* [Breadth-First Search (BFS)](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/graph/breadth-first-search)
33+
* Search
34+
* [Binary Search](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/search/binary-search)
3335
* Sorting
3436
* [Bubble Sort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/bubble-sort)
3537
* [Selection Sort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/selection-sort)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Binary Search
2+
3+
In computer science, binary search, also known as half-interval
4+
search, logarithmic search, or binary chop, is a search algorithm
5+
that finds the position of a target value within a sorted
6+
array. Binary search compares the target value to the middle
7+
element of the array; if they are unequal, the half in which
8+
the target cannot lie is eliminated and the search continues
9+
on the remaining half until it is successful. If the search
10+
ends with the remaining half being empty, the target is not
11+
in the array.
12+
13+
![Binary Search](https://upload.wikimedia.org/wikipedia/commons/8/83/Binary_Search_Depiction.svg)
14+
15+
## References
16+
17+
[Wikipedia](https://en.wikipedia.org/wiki/Binary_search_algorithm)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import binarySearch from '../binarySearch';
2+
3+
describe('binarySearch', () => {
4+
it('should search number in sorted array', () => {
5+
expect(binarySearch([], 1)).toBe(-1);
6+
expect(binarySearch([1], 1)).toBe(0);
7+
expect(binarySearch([1, 2], 1)).toBe(0);
8+
expect(binarySearch([1, 2], 2)).toBe(1);
9+
expect(binarySearch([1, 5, 10, 12], 1)).toBe(0);
10+
expect(binarySearch([1, 5, 10, 12, 14, 17, 22, 100], 17)).toBe(5);
11+
expect(binarySearch([1, 5, 10, 12, 14, 17, 22, 100], 1)).toBe(0);
12+
expect(binarySearch([1, 5, 10, 12, 14, 17, 22, 100], 100)).toBe(7);
13+
expect(binarySearch([1, 5, 10, 12, 14, 17, 22, 100], 0)).toBe(-1);
14+
});
15+
16+
it('should search object in sorted array', () => {
17+
const sortedArrayOfObjects = [
18+
{ key: 1, value: 'value1' },
19+
{ key: 2, value: 'value2' },
20+
{ key: 3, value: 'value3' },
21+
];
22+
23+
const comparator = (a, b) => {
24+
if (a.key === b.key) return 0;
25+
return a.key < b.key ? -1 : 1;
26+
};
27+
28+
expect(binarySearch([], { key: 1 }, comparator)).toBe(-1);
29+
expect(binarySearch(sortedArrayOfObjects, { key: 4 }, comparator)).toBe(-1);
30+
expect(binarySearch(sortedArrayOfObjects, { key: 1 }, comparator)).toBe(0);
31+
expect(binarySearch(sortedArrayOfObjects, { key: 2 }, comparator)).toBe(1);
32+
expect(binarySearch(sortedArrayOfObjects, { key: 3 }, comparator)).toBe(2);
33+
});
34+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import Comparator from '../../../utils/comparator/Comparator';
2+
3+
/**
4+
* @param {*[]} sortedArray
5+
* @param {*} seekElement
6+
* @param {function(a, b)} [comparatorCallback]
7+
* @return {number}
8+
*/
9+
10+
export default function binarySearch(sortedArray, seekElement, comparatorCallback) {
11+
const comparator = new Comparator(comparatorCallback);
12+
13+
let startIndex = 0;
14+
let endIndex = sortedArray.length - 1;
15+
16+
while (startIndex <= endIndex) {
17+
const middleIndex = startIndex + Math.floor((endIndex - startIndex) / 2);
18+
19+
// If we've found the element just return its position.
20+
if (comparator.equal(sortedArray[middleIndex], seekElement)) {
21+
return middleIndex;
22+
}
23+
24+
// Decide which half to choose for seeking next: left or right one.
25+
if (comparator.lessThen(sortedArray[middleIndex], seekElement)) {
26+
// Go to the right half of the array.
27+
startIndex = middleIndex + 1;
28+
} else {
29+
// Go to the left half of the array.
30+
endIndex = middleIndex - 1;
31+
}
32+
}
33+
34+
return -1;
35+
}

0 commit comments

Comments
 (0)