fix: unbiased Fisher-Yates in bogoSort; do not mutate RGB input - #1912
Open
zeeshanzzz788 wants to merge 1 commit into
Open
fix: unbiased Fisher-Yates in bogoSort; do not mutate RGB input#1912zeeshanzzz788 wants to merge 1 commit into
zeeshanzzz788 wants to merge 1 commit into
Conversation
- shuffle() used Math.random() * i and swapped with i-1, which is a biased shuffle that cannot produce all permutations. Use standard Fisher-Yates over [0, i]. - rgbToHsl aliased the input array and overwrote it with HSL values. Copy the input first so callers keep their RGB data. Fixes TheAlgorithms#1867 Fixes TheAlgorithms#1907
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1912 +/- ##
=======================================
Coverage 85.91% 85.91%
=======================================
Files 379 379
Lines 19778 19778
Branches 3016 3015 -1
=======================================
Hits 16993 16993
Misses 2785 2785 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
BogoSort (
Sorts/BogoSort.js)The previous
shuffleusedMath.random() * iand swapped with indexi - 1, which is a biased shuffle (some permutations never occur). Replaced with standard Fisher–Yates over[0, i].RgbHslConversion (
Conversions/RgbHslConversion.js)rgbToHslaliased the input array (let colorHsl = colorRgb) and wrote HSL values into it, destroying the caller’s RGB data. It now copies with.slice()first.Verification
Fixes #1867
Fixes #1907