Skip to content

Commit fc2115d

Browse files
Corrected bogosort shuffle logic
1 parent 08d8c6b commit fc2115d

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

Sorts/BogoSort.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ export function isSorted(array) {
1212
}
1313

1414
/**
15-
* Shuffles the given array randomly in place.
15+
* Shuffles the given array randomly in place using the Fisher–Yates algorithm.
1616
*/
1717
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
18+
for (let i = array.length - 1; i > 0; i--) {
19+
const j = Math.floor(Math.random() * (i + 1));
20+
[array[i], array[j]] = [array[j], array[i]];
2321
}
2422
}
2523

0 commit comments

Comments
 (0)