We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 08d8c6b commit fc2115dCopy full SHA for fc2115d
Sorts/BogoSort.js
@@ -12,14 +12,12 @@ export function isSorted(array) {
12
}
13
14
/**
15
- * Shuffles the given array randomly in place.
+ * Shuffles the given array randomly in place using the Fisher–Yates algorithm.
16
*/
17
function shuffle(array) {
18
- for (let i = array.length - 1; i; i--) {
19
- const m = Math.floor(Math.random() * i)
20
- const n = array[i - 1]
21
- array[i - 1] = array[m]
22
- array[m] = n
+ for (let i = array.length - 1; i > 0; i--) {
+ const j = Math.floor(Math.random() * (i + 1));
+ [array[i], array[j]] = [array[j], array[i]];
23
24
25
0 commit comments