From f01c901baf4611fea0bdadb637c50cd944e081e1 Mon Sep 17 00:00:00 2001 From: Leandre-B Date: Wed, 9 Jul 2025 16:28:29 +0200 Subject: [PATCH] fix: replace suffle by Fisher-Yates for uniform randomness --- sorting/bogo_sort.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sorting/bogo_sort.cpp b/sorting/bogo_sort.cpp index 749eaddd065..0665b974ed4 100644 --- a/sorting/bogo_sort.cpp +++ b/sorting/bogo_sort.cpp @@ -34,10 +34,11 @@ namespace sorting { * @returns new array with elements shuffled from a given array */ template +//Fisher-Yates shuffle std::array shuffle (std::array arr) { - for (int i = 0; i < N; i++) { - // Swaps i'th index with random index (less than array size) - std::swap(arr[i], arr[std::rand() % N]); + for (int i = N-1; i >=0; i--) { + // Swaps i'th index with random index (less or equal than i) + std::swap(arr[i], arr[std::rand() % (i+1)]); } return arr; }