From 2b8b0d31992aa0d00d4629ad8c8d25f64ae0a8d7 Mon Sep 17 00:00:00 2001 From: ayushchaudhari562 Date: Wed, 20 May 2026 18:09:23 +0530 Subject: [PATCH] fix: correct Fisher-Yates shuffle bias in bogoSort (#1867) --- Sorts/BogoSort.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Sorts/BogoSort.js b/Sorts/BogoSort.js index eeb4f7feeb..8643e61d08 100644 --- a/Sorts/BogoSort.js +++ b/Sorts/BogoSort.js @@ -15,11 +15,9 @@ export function isSorted(array) { * Shuffles the given array randomly in place. */ 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]] } }