Skip to content

Commit c77c903

Browse files
committed
[Algorithm Design and Techniques]
1 parent 5eca6f2 commit c77c903

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Work in Progress.
2222
* 10: [Heap](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter10)
2323
* 11: [Graphs](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter11)
2424
* 12: [Sorting and Searching Algorithms](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter12)
25+
* 13: [String and Math Algorithms](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter13)
26+
* 14: [Algorithm Design and Techniques](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter14)
27+
* 15: [Algorithm Complexity](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter15)
2528

2629
### Third Edition Updates
2730

examples/PacktDataStructuresAlgorithms.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Compare, defaultCompare, DOES_NOT_EXIST } from '../../util';
2+
import { quickSort } from '../sorting/quicksort';
3+
4+
function binarySearchRecursive<T>(
5+
array: T[],
6+
value: T,
7+
low: number,
8+
high: number,
9+
compareFn = defaultCompare
10+
): number {
11+
if (low <= high) {
12+
const mid = Math.floor((low + high) / 2);
13+
const element = array[mid];
14+
15+
if (compareFn(element, value) === Compare.LESS_THAN) {
16+
return binarySearchRecursive(array, value, mid + 1, high, compareFn);
17+
} else if (compareFn(element, value) === Compare.BIGGER_THAN) {
18+
return binarySearchRecursive(array, value, low, mid - 1, compareFn);
19+
} else {
20+
return mid;
21+
}
22+
}
23+
return DOES_NOT_EXIST;
24+
}
25+
26+
export function binarySearch<T>(array: T[], value: T, compareFn = defaultCompare) {
27+
const sortedArray = quickSort(array);
28+
const low = 0;
29+
const high = sortedArray.length - 1;
30+
31+
return binarySearchRecursive(array, value, low, high, compareFn);
32+
}

src/ts/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ export { sequentialSearch as sequentialSearch } from './algorithms/search/sequen
6060
export { findMaxValue as findMaxValue } from './algorithms/search/min-max-search';
6161
export { findMinValue as findMinValue } from './algorithms/search/min-max-search';
6262

63+
// chapter 14
64+
export { binarySearch as binarySearchRecursive } from './algorithms/search/binary-search-recursive';
6365

6466

6567
/* import { hotPotato } from './others/hot-potato';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { binarySearchRecursive } from '../../../../src/ts/index';
2+
import { testSearchAlgorithm } from './search-algorithms-tests';
3+
4+
testSearchAlgorithm(binarySearchRecursive, 'Binary Search Recursive');
5+

0 commit comments

Comments
 (0)