Skip to content

Commit e63709a

Browse files
committed
Add Fisher-Yates.
1 parent 3aa8068 commit e63709a

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@
3333
* [Power Set](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/power-set)
3434
* [Primality Test](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/primality-test) (trial division)
3535
* [Euclidean Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/euclidean-algorithm) - calculate the greatest common divisor (GCD)
36+
* [Fisher–Yates Shuffle](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/fisher-yates) - random permutation of a finite sequence
3637
* Collatz Conjecture algorithm
3738
* Extended Euclidean algorithm
38-
* Find Divisors
39-
* Fisher-Yates
4039
* Greatest Difference
4140
* Least Common Multiple
4241
* Newton's square
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Fisher–Yates shuffle
2+
3+
The Fisher–Yates shuffle is an algorithm for generating a random
4+
permutation of a finite sequence—in plain terms, the algorithm
5+
shuffles the sequence. The algorithm effectively puts all the
6+
elements into a hat; it continually determines the next element
7+
by randomly drawing an element from the hat until no elements
8+
remain. The algorithm produces an unbiased permutation: every
9+
permutation is equally likely. The modern version of the
10+
algorithm is efficient: it takes time proportional to the
11+
number of items being shuffled and shuffles them in place.
12+
13+
## References
14+
15+
[Wikipedia](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import fisherYates from '../fisherYates';
2+
import { sortedArr } from '../../../sorting/SortTester';
3+
import QuickSort from '../../../sorting/quick-sort/QuickSort';
4+
5+
describe('fisherYates', () => {
6+
it('should shuffle small arrays', () => {
7+
expect(fisherYates([])).toEqual([]);
8+
expect(fisherYates([1])).toEqual([1]);
9+
});
10+
11+
it('should shuffle array randomly', () => {
12+
const shuffledArray = fisherYates(sortedArr);
13+
const sorter = new QuickSort();
14+
15+
expect(shuffledArray.length).toBe(sortedArr.length);
16+
expect(shuffledArray).not.toEqual(sortedArr);
17+
expect(sorter.sort(shuffledArray)).toEqual(sortedArr);
18+
});
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {*[]} originalArray
3+
* @return {*[]}
4+
*/
5+
export default function fisherYates(originalArray) {
6+
// Clone array from preventing original array from modification (for testing purpose).
7+
const array = originalArray.slice(0);
8+
9+
if (array.length <= 1) {
10+
return array;
11+
}
12+
13+
for (let i = (array.length - 1); i > 0; i -= 1) {
14+
const randomIndex = Math.floor(Math.random() * (i + 1));
15+
const tmp = array[randomIndex];
16+
array[randomIndex] = array[i];
17+
array[i] = tmp;
18+
}
19+
20+
return array;
21+
}

0 commit comments

Comments
 (0)