Skip to content

Commit f3fa1ba

Browse files
committed
Fisher-Yates shuffling algorithm included
1 parent 30f4ab8 commit f3fa1ba

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/shuffle/fisheryates.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* The shuffling algorithm of
3+
* Fisher-Yates. Complexity O(n)
4+
*
5+
* @param {array} array The array which should be shuffled
6+
* @return {array} The shuffled array.
7+
*/
8+
function shuffle(array) {
9+
var size = array.length,
10+
rand, temp;
11+
for (var i = 1; i < size; i += 1) {
12+
rand = Math.round(Math.random() * i);
13+
temp = array[rand];
14+
array[rand] = array[i];
15+
array[i] = temp;
16+
}
17+
return array;
18+
}
19+
20+
//var array = [1,2,3,4,5,6,7,8,9];
21+
//console.log(array);
22+
//console.log(shuffle(array));

0 commit comments

Comments
 (0)