File tree 4 files changed +56
-2
lines changed
src/algorithms/math/fisher-yates
4 files changed +56
-2
lines changed Original file line number Diff line number Diff line change 33
33
* [ Power Set] ( https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/power-set )
34
34
* [ Primality Test] ( https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/primality-test ) (trial division)
35
35
* [ 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
36
37
* Collatz Conjecture algorithm
37
38
* Extended Euclidean algorithm
38
- * Find Divisors
39
- * Fisher-Yates
40
39
* Greatest Difference
41
40
* Least Common Multiple
42
41
* Newton's square
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments