diff --git a/Sorts/BogoSort.js b/Sorts/BogoSort.js index eeb4f7feeb..d45a82268c 100644 --- a/Sorts/BogoSort.js +++ b/Sorts/BogoSort.js @@ -12,14 +12,12 @@ export function isSorted(array) { } /** - * Shuffles the given array randomly in place. + * Shuffles the given array randomly in place using the Fisher–Yates algorithm. */ function shuffle(array) { - for (let i = array.length - 1; i; i--) { - const m = Math.floor(Math.random() * i) - const n = array[i - 1] - array[i - 1] = array[m] - 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]]; } }