Skip to content

Commit 3f810dc

Browse files
committed
Add binary search algorithm implementation with tests and docs
1 parent 590f3ce commit 3f810dc

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@ Linear search is a searching algorithm that sequentially checks each element in
66

77
- **Best Case:** **O(1)** (Element found at the beginning)
88
- **Worst Case:** **O(n)** (Element at the end or not present)
9+
10+
# Binary Search
11+
12+
Binary search is a searching algorithm that find the desiired element in a **sorted** sequence by repeatedly dividing the search interval in half until the target is found or the interval is empty.
13+
14+
## Time Complexity
15+
16+
- **Best Case:** **O(1)** (Element is in the middle)
17+
- **Worst Case:** **O(log n)** (Element at the end or not present)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export const binarySearch = <T>(
2+
array: T[],
3+
target: T
4+
): { index: number; element: T } | null => {
5+
const search = (leftIndex: number, rightIndex: number) => {
6+
if (leftIndex > rightIndex) return null
7+
8+
const midIndex = Math.floor((leftIndex + rightIndex) / 2)
9+
const midElement = array[midIndex]
10+
11+
if (target === midElement)
12+
return { index: midIndex, element: midElement }
13+
if (target > midElement) return search(midIndex + 1, rightIndex)
14+
return search(leftIndex, midIndex - 1)
15+
}
16+
return search(0, array.length - 1)
17+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { describe, it, expect } from 'vitest'
2+
import { binarySearch } from '../../../src/algorithms/searching/binarySearch'
3+
4+
describe('binarySearch', () => {
5+
describe('when array is empty', () => {
6+
it('should return null', () => {
7+
expect(binarySearch([], 1)).toBeNull()
8+
})
9+
})
10+
11+
describe('when the array has even number of elements', () => {
12+
describe('and target is in the list', () => {
13+
const vectors = Array.from({ length: 10 }, (_, index) => ({
14+
array: Array.from({ length: 10 }, (_, index) => index + 1),
15+
target: index + 1,
16+
expected: { index, element: index + 1 }
17+
}))
18+
19+
it('should return the target with its index', () => {
20+
vectors.forEach(({ array, target, expected }) => {
21+
expect(binarySearch(array, target)).toStrictEqual(expected)
22+
})
23+
})
24+
})
25+
26+
describe('and target is not in the list', () => {
27+
const vectors = Array.from({ length: 10 }, (_, index) => ({
28+
array: Array.from({ length: 10 }, (_, index) => index + 1),
29+
target: 100
30+
}))
31+
32+
it('should return null', () => {
33+
vectors.forEach(({ array, target }) => {
34+
expect(binarySearch(array, target)).toBeNull()
35+
})
36+
})
37+
})
38+
})
39+
40+
describe('when the array has odd number of elements', () => {
41+
describe('and target is in the list', () => {
42+
const vectors = Array.from({ length: 11 }, (_, index) => ({
43+
array: Array.from({ length: 11 }, (_, index) => index + 1),
44+
target: index + 1,
45+
expected: { index, element: index + 1 }
46+
}))
47+
48+
it('should return the target with its index', () => {
49+
vectors.forEach(({ array, target, expected }) => {
50+
expect(binarySearch(array, target)).toStrictEqual(expected)
51+
})
52+
})
53+
})
54+
55+
describe('and target is not in the list', () => {
56+
const vectors = Array.from({ length: 11 }, (_, index) => ({
57+
array: Array.from({ length: 11 }, (_, index) => index + 1),
58+
target: 100
59+
}))
60+
61+
it('should return null', () => {
62+
vectors.forEach(({ array, target }) => {
63+
expect(binarySearch(array, target)).toBeNull()
64+
})
65+
})
66+
})
67+
})
68+
})

0 commit comments

Comments
 (0)