Skip to content

Commit 2b8b0d3

Browse files
fix: correct Fisher-Yates shuffle bias in bogoSort (#1867)
1 parent 5c39e87 commit 2b8b0d3

1 file changed

Lines changed: 3 additions & 5 deletions

File tree

Sorts/BogoSort.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ export function isSorted(array) {
1515
* Shuffles the given array randomly in place.
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)