Skip to content
This repository was archived by the owner on Mar 27, 2022. It is now read-only.

Commit b9aeb98

Browse files
committed
Rewrite all
1 parent 42faf96 commit b9aeb98

16 files changed

+137
-254
lines changed

__tests__/binarySearch.spec.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

__tests__/insertion.spec.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
import { SortedArray } from '../src/SortedArray';
2-
import { defaultComparator } from "../src/helpers";
1+
import { insert, numberCmp } from '../src';
32

4-
describe('Insertion in sorted array', () => {
5-
const sortedArray = new SortedArray([1, 3, 4], defaultComparator);
3+
describe('Insertion', () => {
4+
it('should insert new element', () => {
5+
const array = [0, 1, 3, 4];
6+
const EXPECTED = [0, 1, 2, 3, 4];
67

7-
it('', () => {
8-
const EXPECTED_INDEX = 1;
9-
const index = sortedArray.insert(2);
8+
insert(array, 2, numberCmp);
109

11-
expect(index).toBe(EXPECTED_INDEX);
10+
expect(array).toEqual(EXPECTED);
1211
})
13-
})
12+
13+
it('correct insert in first and last position', () => {
14+
const array = [1, 2, 3];
15+
const EXPECTED = [0, 1, 2, 3, 4];
16+
17+
insert(array, 0, numberCmp);
18+
insert(array, 4, numberCmp);
19+
20+
expect(array).toEqual(EXPECTED);
21+
})
22+
})

__tests__/searching.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { search, numberCmp } from '../src';
2+
3+
describe('Searching', () => {
4+
it('should return index of element', () => {
5+
const array = [0, 1, 2, 3, 4, 5];
6+
const EXPECTED = 3;
7+
8+
const index = search(array, 3, numberCmp);
9+
10+
expect(index).toBe(EXPECTED);
11+
})
12+
13+
it('should return -1 if element is not found', () => {
14+
const array = [0, 1, 2, 3, 4, 5];
15+
const EXPECTED = -1;
16+
17+
const index = search(array, 10, numberCmp);
18+
19+
expect(index).toBe(EXPECTED);
20+
})
21+
})

__tests__/sorting.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { sort, numberCmp } from '../src';
2+
3+
describe('Sorting', () => {
4+
it('should sort array of numbers', () => {
5+
const array = [ 2, 5, 1, 0, 4, 3, 6 ];
6+
const EXPECTED = [ 0, 1, 2, 3, 4, 5, 6 ];
7+
8+
const sortedArray = sort(array, numberCmp);
9+
10+
expect(sortedArray).toEqual(EXPECTED);
11+
})
12+
})

src/SortedArray.ts

Lines changed: 0 additions & 152 deletions
This file was deleted.

src/cmp.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Comparator } from './types';
2+
3+
export const numberCmp: Comparator<number> = (a, b) => {
4+
if (a === b) {
5+
return 0;
6+
}
7+
8+
return a < b ? -1 : 1;
9+
}

src/helpers/binarySearch.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/helpers/defaultComparator.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/helpers/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export { sort } from './sort';
2+
export { insert } from './insert';
3+
export { search } from './search';
4+
export { numberCmp } from './cmp';
5+
export type { Comparator } from './types';

src/insert.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { Comparator } from './types';
2+
import { internalSearch } from './search';
3+
4+
export function insert<T>(array: T[], element: T, cmp: Comparator<T>): number {
5+
const { index } = internalSearch(array, element, cmp);
6+
7+
let top = array.push(element) - 1;
8+
9+
while (top > index) {
10+
array[top] = array[top - 1];
11+
array[top - 1] = element;
12+
13+
top--
14+
}
15+
16+
return index;
17+
}

src/search.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { Comparator } from './types';
2+
3+
type InternalSearchResult = {
4+
index: number;
5+
found: boolean;
6+
}
7+
8+
export function search<T>(array: T[], element: T, cmp: Comparator<T>): number {
9+
const { index, found } = internalSearch(array, element, cmp);
10+
11+
return found ? index : -1;
12+
}
13+
14+
export function internalSearch<T>(array: T[], element: T, cmp: Comparator<T>): InternalSearchResult {
15+
let right = array.length - 1;
16+
let left = 0;
17+
let mid = 0;
18+
let ord = 0;
19+
20+
while (right >= left) {
21+
mid = (left + right) / 2 >>> 0;
22+
ord = cmp(element, array[mid]);
23+
24+
if (ord === 0) {
25+
return {
26+
index: mid,
27+
found: true,
28+
}
29+
}
30+
31+
if (ord === 1) {
32+
left = mid + 1;
33+
} else {
34+
right = mid - 1;
35+
}
36+
}
37+
38+
return {
39+
index: left,
40+
found: false,
41+
}
42+
}

0 commit comments

Comments
 (0)