Skip to content

fix: correct Fisher-Yates shuffle bias in bogoSort (#1867) - #1899

Open
ayushchaudhari562 wants to merge 1 commit into
TheAlgorithms:masterfrom
ayushchaudhari562:fix-bogosort-shuffle
Open

fix: correct Fisher-Yates shuffle bias in bogoSort (#1867)#1899
ayushchaudhari562 wants to merge 1 commit into
TheAlgorithms:masterfrom
ayushchaudhari562:fix-bogosort-shuffle

Conversation

@ayushchaudhari562

Copy link
Copy Markdown

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Description:

Fixes #1867

This PR addresses the mathematical bias and off-by-one errors in the shuffle function inside the bogoSort implementation.

  1. Corrected the loop bounds so that the last element of the array is no longer skipped during the shuffle process.
  2. Updated the random index generation range from [0, i) to [0, i] by using Math.random() * (i + 1). This ensures an unbiased Fisher-Yates shuffle where elements are allowed to swap with themselves, restoring proper uniform distribution.
  3. Updated the code to use clean, modern ES6 array destructuring for the variable swapping logic.

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new JavaScript files are placed inside an existing directory.
  • All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames. Example: User个人资料.js is allowed but userprofile.js, User个人资料.js , user-个人资料.js, user_profile.js are not
  • All new algorithms have a URL in their comments that points to 维基pedia or another similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@codecov-commenter

codecov-commenter commented May 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.91%. Comparing base (5c39e87) to head (2b8b0d3).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1899      +/-   ##
==========================================
- Coverage   85.91%   85.91%   -0.01%     
==========================================
  Files         379      379              
  Lines       19778    19776       -2     
  分支     3016     3016              
==========================================
- Hits        16993    16991       -2     
  Misses       2785     2785              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 新建 features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

注册 for free to join this conversation on GitHub. Already have an account? 登录 to comment

标签

None yet

项目

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Biased shuffle implementation in bogoSort (incorrect Fisher–Yates range)

3 participants