Skip to content

Commit ab2dbaa

Browse files
committed
[Algorithm Design and Techniques]
1 parent aac21d0 commit ab2dbaa

10 files changed

+69
-6
lines changed

examples/PacktDataStructuresAlgorithms.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/PacktDataStructuresAlgorithms.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
const { bubbleSort } = PacktDataStructuresAlgorithms;
1+
const { minCoinChange } = PacktDataStructuresAlgorithms;
2+
3+
console.log(minCoinChange([1, 5, 10], 15)); // [5, 10]
4+
console.log(minCoinChange([1, 3, 4], 6)); // [3, 3]
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { minCoinChangeGreedy } = PacktDataStructuresAlgorithms;
2+
3+
console.log(minCoinChangeGreedy([1, 5, 10], 15)); // [5, 10]
4+
console.log(minCoinChangeGreedy([1, 3, 4], 6)); // [4, 1, 1]
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { knapSack } = PacktDataStructuresAlgorithms;
2+
3+
const values = [3,4,5];
4+
const weights = [2,3,4];
5+
const capacity = 5;
6+
const n = values.length;
7+
8+
console.log(knapSack(capacity, weights, values, n));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { knapSackRecursive } = PacktDataStructuresAlgorithms;
2+
3+
const values = [3,4,5];
4+
const weights = [2,3,4];
5+
const capacity = 5;
6+
const n = values.length;
7+
8+
console.log(knapSackRecursive(capacity, weights, values, n));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const { knapSackGreedy } = PacktDataStructuresAlgorithms;
2+
3+
const values = [3,4,5];
4+
const weights = [2,3,4];
5+
const capacity = 5;
6+
7+
console.log(knapSackGreedy(capacity, weights, values));

src/js/algorithms/dynamic-programing/knapsack.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ function findValues(n, capacity, kS) {
44
// console.log('Items that are part of the solution:');
55
while (i > 0 && k > 0) {
66
if (kS[i][k] !== kS[i - 1][k]) {
7-
/* console.log(
7+
console.log(
88
'item ' + i + ' can be part of solution w,v: ' + weights[i - 1] + ',' + values[i - 1]
9-
); */
9+
);
1010
i--;
1111
k -= kS[i][k];
1212
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Compare, defaultCompare, DOES_NOT_EXIST } from '../../util';
2+
import { quickSort } from '../sorting/quicksort';
3+
4+
function binarySearchRecursive(array, value, low, high, compareFn = defaultCompare) {
5+
if (low <= high) {
6+
const mid = Math.floor((low + high) / 2);
7+
const element = array[mid];
8+
if (compareFn(element, value) === Compare.LESS_THAN) {
9+
return binarySearchRecursive(array, value, mid + 1, high, compareFn);
10+
} else if (compareFn(element, value) === Compare.BIGGER_THAN) {
11+
return binarySearchRecursive(array, value, low, mid - 1, compareFn);
12+
} else {
13+
return mid;
14+
}
15+
}
16+
return DOES_NOT_EXIST;
17+
}
18+
19+
export function binarySearch(array, value, compareFn = defaultCompare) {
20+
const sortedArray = quickSort(array);
21+
const low = 0;
22+
const high = sortedArray.length - 1;
23+
return binarySearchRecursive(array, value, low, high, compareFn);
24+
}

src/js/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,14 @@ export { findMaxValue } from './algorithms/search/min-max-search';
8383
export { findMinValue } from './algorithms/search/min-max-search';
8484

8585
// chapter 14
86-
// export { binarySearch as binarySearchRecursive } from './algorithms/search/binary-search-recursive';
86+
export { binarySearch as binarySearchRecursive } from './algorithms/search/binary-search-recursive';
8787
export { minCoinChange } from './algorithms/dynamic-programing/min-coin-change';
88+
export { minCoinChange as minCoinChangeGreedy } from './algorithms/greedy/min-coin-change';
89+
export { knapSack } from './algorithms/dynamic-programing/knapsack';
90+
export { knapSack as knapSackRecursive } from './algorithms/dynamic-programing/knapsack-recursive';
91+
export { knapSack as knapSackGreedy } from './algorithms/greedy/knapsack';
92+
export { lcs } from './algorithms/dynamic-programing/longest-common-subsequence';
93+
export { lcs as lcsPrint } from './algorithms/dynamic-programing/longest-common-subsequence-print';
94+
export { lcs as lcsRecursive } from './algorithms/greedy/longest-common-subsequence';
8895
export { ratInAMaze } from './algorithms/backtracking/rat-in-maze';
8996
export { sudokuSolver } from './algorithms/backtracking/sudoku-solver';

0 commit comments

Comments
 (0)