-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharray.big-o.test.ts
More file actions
31 lines (26 loc) · 867 Bytes
/
Copy patharray.big-o.test.ts
File metadata and controls
31 lines (26 loc) · 867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { binarySearch } from '../algos/binarySearch';
describe('array Big-O time', () => {
it('returns value at O(1)', () => {
expect([1, 2, 3][0]).toBe(1);
});
it('returns value at O(n)', () => {
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
expect(arr.find((value, i, a) => a[i] === 7)).toBe(7);
});
it('returns value at O(log n) for binary search', () => {
// preprocessing (sorting) data makes subsequent queries efficient, from O(n) to O(log n)
const arr = [2, 1, 3, 5, 4, 6, 7, 8, 9].sort();
expect(binarySearch(arr, 4)).toBe(3);
expect(binarySearch(arr, 2)).toBe(1);
});
it('push is O(1)', () => {
const arr = [1, 2, 3];
arr.push(4);
expect(arr).toEqual([1, 2, 3, 4]);
});
it('unshift is O(n)', () => {
const arr = [1, 2, 3];
arr.unshift(0);
expect(arr).toEqual([0, 1, 2, 3]);
});
});