From fc2115dd75d834f510c6938a73031936da99a21f Mon Sep 17 00:00:00 2001 From: Sukalyan Sahu Date: Thu, 22 Jan 2026 19:45:47 +0530 Subject: [PATCH] Corrected bogosort shuffle logic --- Sorts/BogoSort.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) 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]]; } }