mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Compare commits
5 Commits
bd37988dc3
...
76d8ad6fbe
Author | SHA1 | Date | |
---|---|---|---|
|
76d8ad6fbe | ||
|
45762593b5 | ||
|
ceed279de8 | ||
|
b5ca4b12e8 | ||
|
677146717d |
@ -92,6 +92,7 @@ a set of rules that precisely define a sequence of operations.
|
||||
* `B` [Pascal's Triangle](src/algorithms/math/pascal-triangle)
|
||||
* `B` [Complex Number](src/algorithms/math/complex-number) - complex numbers and basic operations with them
|
||||
* `B` [Radian & Degree](src/algorithms/math/radian) - radians to degree and backwards conversion
|
||||
* `B` [Mini-Max Sum](src/algorithms/math/mini-max-sum)
|
||||
* `B` [Fast Powering](src/algorithms/math/fast-powering)
|
||||
* `B` [Horner's method](src/algorithms/math/horner-method) - polynomial evaluation
|
||||
* `B` [Matrices](src/algorithms/math/matrix) - matrices and basic matrix operations (multiplication, transposition, etc.)
|
||||
|
27
src/algorithms/math/mini-max/MiniMax.js
Normal file
27
src/algorithms/math/mini-max/MiniMax.js
Normal file
@ -0,0 +1,27 @@
|
||||
|
||||
/**
|
||||
*
|
||||
* @param numbers - array of numbers
|
||||
* @return [number - Max, number - Min]
|
||||
*/
|
||||
export default function miniMaxSum(numbers) {
|
||||
if (!Array.isArray(numbers) || numbers.length === 0) {
|
||||
return [null, null];
|
||||
}
|
||||
|
||||
let min = numbers[0];
|
||||
let max = numbers[0];
|
||||
let sum = numbers[0];
|
||||
|
||||
for (let i = 1; i < numbers.length; i += 1) {
|
||||
sum += numbers[i];
|
||||
|
||||
if (numbers[i] > max) {
|
||||
max = numbers[i];
|
||||
} else if (numbers[i] < min) {
|
||||
min = numbers[i];
|
||||
}
|
||||
}
|
||||
|
||||
return [sum - max, sum - min];
|
||||
}
|
12
src/algorithms/math/mini-max/Readme.md
Normal file
12
src/algorithms/math/mini-max/Readme.md
Normal file
@ -0,0 +1,12 @@
|
||||
|
||||
# Mini-Max Sum
|
||||
|
||||
Given an array of five positive integers, find the minimum and maximum values that
|
||||
can be calculated by summing exactly four of the array integers. Then return the
|
||||
respective minimum and maximum values as a array of numbers
|
||||
|
||||
|
||||
```
|
||||
[1, 2, 3, 4, 5] = [10, 14]
|
||||
[1, 3, 5, 7, 9] = [16, 24]
|
||||
```
|
13
src/algorithms/math/mini-max/__test__/miniMax.test.js
Normal file
13
src/algorithms/math/mini-max/__test__/miniMax.test.js
Normal file
@ -0,0 +1,13 @@
|
||||
import miniMaxSum from '../miniMaxSum';
|
||||
|
||||
describe('miniMaxSum', () => {
|
||||
it('should calculate miniMaxSum', () => {
|
||||
expect(miniMaxSum([1, 3, 5, 7, 9])).toStrictEqual([16, 24]);
|
||||
expect(miniMaxSum([1, 2, 3, 4, 5])).toStrictEqual([10, 14]);
|
||||
expect(miniMaxSum([-1, -2, 3, 4, 5])).toStrictEqual([4, 11]);
|
||||
expect(miniMaxSum([0, 1, 2, 3, 4, 5])).toStrictEqual([10, 15]);
|
||||
expect(miniMaxSum([])).toStrictEqual([null, null]);
|
||||
expect(miniMaxSum(12)).toStrictEqual([null, null]);
|
||||
expect(miniMaxSum(null)).toStrictEqual([null, null]);
|
||||
});
|
||||
});
|
@ -4,17 +4,17 @@ describe('linearSearch', () => {
|
||||
it('should search all numbers in array', () => {
|
||||
const array = [1, 2, 4, 6, 2];
|
||||
|
||||
expect(linearSearch(array, 10)).toEqual([]);
|
||||
expect(linearSearch(array, 1)).toEqual([0]);
|
||||
expect(linearSearch(array, 2)).toEqual([1, 4]);
|
||||
expect(linearSearch(array, 10)).toEqual(-1);
|
||||
expect(linearSearch(array, 1)).toEqual(0);
|
||||
expect(linearSearch(array, 2)).toEqual(1);
|
||||
});
|
||||
|
||||
it('should search all strings in array', () => {
|
||||
const array = ['a', 'b', 'a'];
|
||||
|
||||
expect(linearSearch(array, 'c')).toEqual([]);
|
||||
expect(linearSearch(array, 'b')).toEqual([1]);
|
||||
expect(linearSearch(array, 'a')).toEqual([0, 2]);
|
||||
expect(linearSearch(array, 'c')).toEqual(-1);
|
||||
expect(linearSearch(array, 'b')).toEqual(1);
|
||||
expect(linearSearch(array, 'a')).toEqual(0);
|
||||
});
|
||||
|
||||
it('should search through objects as well', () => {
|
||||
@ -33,8 +33,8 @@ describe('linearSearch', () => {
|
||||
{ key: 6 },
|
||||
];
|
||||
|
||||
expect(linearSearch(array, { key: 10 }, comparatorCallback)).toEqual([]);
|
||||
expect(linearSearch(array, { key: 5 }, comparatorCallback)).toEqual([0]);
|
||||
expect(linearSearch(array, { key: 6 }, comparatorCallback)).toEqual([1, 3]);
|
||||
expect(linearSearch(array, { key: 10 }, comparatorCallback)).toEqual(-1);
|
||||
expect(linearSearch(array, { key: 5 }, comparatorCallback)).toEqual(0);
|
||||
expect(linearSearch(array, { key: 6 }, comparatorCallback)).toEqual(1);
|
||||
});
|
||||
});
|
@ -10,13 +10,12 @@ import Comparator from '../../../utils/comparator/Comparator';
|
||||
*/
|
||||
export default function linearSearch(array, seekElement, comparatorCallback) {
|
||||
const comparator = new Comparator(comparatorCallback);
|
||||
const foundIndices = [];
|
||||
|
||||
array.forEach((element, index) => {
|
||||
if (comparator.equal(element, seekElement)) {
|
||||
foundIndices.push(index);
|
||||
for (let i = 0; i < array.length; i += 1) {
|
||||
if (comparator.equal(array[i], seekElement)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return foundIndices;
|
||||
return -1;
|
||||
}
|
@ -8,30 +8,28 @@ export default class InsertionSort extends Sort {
|
||||
for (let i = 1; i < array.length; i += 1) {
|
||||
let currentIndex = i;
|
||||
|
||||
const temp = array[currentIndex];
|
||||
|
||||
// Call visiting callback.
|
||||
this.callbacks.visitingCallback(array[i]);
|
||||
|
||||
// Check if previous element is greater than current element.
|
||||
// If so, swap the two elements.
|
||||
while (
|
||||
array[currentIndex - 1] !== undefined
|
||||
&& this.comparator.lessThan(array[currentIndex], array[currentIndex - 1])
|
||||
currentIndex > 0
|
||||
&& this.comparator.lessThan(temp, array[currentIndex - 1])
|
||||
) {
|
||||
// Call visiting callback.
|
||||
this.callbacks.visitingCallback(array[currentIndex - 1]);
|
||||
|
||||
// Swap the elements.
|
||||
[
|
||||
array[currentIndex - 1],
|
||||
array[currentIndex],
|
||||
] = [
|
||||
array[currentIndex],
|
||||
array[currentIndex - 1],
|
||||
];
|
||||
array[currentIndex] = array[currentIndex - 1];
|
||||
|
||||
// Shift current index left.
|
||||
currentIndex -= 1;
|
||||
}
|
||||
|
||||
array[currentIndex] = temp;
|
||||
}
|
||||
|
||||
return array;
|
||||
|
Loading…
Reference in New Issue
Block a user