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: UserProfile.js is allowed but userprofile.js, UserProfile.js , user-Profile.js, user_profile.js are not
  • All new algorithms have a URL in their comments that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@codecov-commenter
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     
  Branches     3016     3016              
==========================================
- Hits        16993    16991       -2     
  Misses       2785     2785              

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

🚀 New 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants