diff --git a/sorting/bogo_sort.cpp b/sorting/bogo_sort.cpp index 749eaddd06..0665b974ed 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; }