mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Add Fisher-Yates.
This commit is contained in:
parent
3aa80688bb
commit
e63709a271
@ -33,10 +33,9 @@
|
|||||||
* [Power Set](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/power-set)
|
* [Power Set](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/power-set)
|
||||||
* [Primality Test](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/primality-test) (trial division)
|
* [Primality Test](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/primality-test) (trial division)
|
||||||
* [Euclidean Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/euclidean-algorithm) - calculate the greatest common divisor (GCD)
|
* [Euclidean Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/euclidean-algorithm) - calculate the greatest common divisor (GCD)
|
||||||
|
* [Fisher–Yates Shuffle](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/fisher-yates) - random permutation of a finite sequence
|
||||||
* Collatz Conjecture algorithm
|
* Collatz Conjecture algorithm
|
||||||
* Extended Euclidean algorithm
|
* Extended Euclidean algorithm
|
||||||
* Find Divisors
|
|
||||||
* Fisher-Yates
|
|
||||||
* Greatest Difference
|
* Greatest Difference
|
||||||
* Least Common Multiple
|
* Least Common Multiple
|
||||||
* Newton's square
|
* Newton's square
|
||||||
|
15
src/algorithms/math/fisher-yates/README.md
Normal file
15
src/algorithms/math/fisher-yates/README.md
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
# Fisher–Yates shuffle
|
||||||
|
|
||||||
|
The Fisher–Yates shuffle is an algorithm for generating a random
|
||||||
|
permutation of a finite sequence—in plain terms, the algorithm
|
||||||
|
shuffles the sequence. The algorithm effectively puts all the
|
||||||
|
elements into a hat; it continually determines the next element
|
||||||
|
by randomly drawing an element from the hat until no elements
|
||||||
|
remain. The algorithm produces an unbiased permutation: every
|
||||||
|
permutation is equally likely. The modern version of the
|
||||||
|
algorithm is efficient: it takes time proportional to the
|
||||||
|
number of items being shuffled and shuffles them in place.
|
||||||
|
|
||||||
|
## References
|
||||||
|
|
||||||
|
[Wikipedia](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle)
|
@ -0,0 +1,19 @@
|
|||||||
|
import fisherYates from '../fisherYates';
|
||||||
|
import { sortedArr } from '../../../sorting/SortTester';
|
||||||
|
import QuickSort from '../../../sorting/quick-sort/QuickSort';
|
||||||
|
|
||||||
|
describe('fisherYates', () => {
|
||||||
|
it('should shuffle small arrays', () => {
|
||||||
|
expect(fisherYates([])).toEqual([]);
|
||||||
|
expect(fisherYates([1])).toEqual([1]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should shuffle array randomly', () => {
|
||||||
|
const shuffledArray = fisherYates(sortedArr);
|
||||||
|
const sorter = new QuickSort();
|
||||||
|
|
||||||
|
expect(shuffledArray.length).toBe(sortedArr.length);
|
||||||
|
expect(shuffledArray).not.toEqual(sortedArr);
|
||||||
|
expect(sorter.sort(shuffledArray)).toEqual(sortedArr);
|
||||||
|
});
|
||||||
|
});
|
21
src/algorithms/math/fisher-yates/fisherYates.js
Normal file
21
src/algorithms/math/fisher-yates/fisherYates.js
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
/**
|
||||||
|
* @param {*[]} originalArray
|
||||||
|
* @return {*[]}
|
||||||
|
*/
|
||||||
|
export default function fisherYates(originalArray) {
|
||||||
|
// Clone array from preventing original array from modification (for testing purpose).
|
||||||
|
const array = originalArray.slice(0);
|
||||||
|
|
||||||
|
if (array.length <= 1) {
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = (array.length - 1); i > 0; i -= 1) {
|
||||||
|
const randomIndex = Math.floor(Math.random() * (i + 1));
|
||||||
|
const tmp = array[randomIndex];
|
||||||
|
array[randomIndex] = array[i];
|
||||||
|
array[i] = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user