Skip to content

fix: unbiased Fisher-Yates in bogoSort; do not mutate RGB input - #1912

Open
zeeshanzzz788 wants to merge 1 commit into
TheAlgorithms:masterfrom
zeeshanzzz788:fix/bogosort-fisher-yates-and-rgb-mutation
Open

fix: unbiased Fisher-Yates in bogoSort; do not mutate RGB input#1912
zeeshanzzz788 wants to merge 1 commit into
TheAlgorithms:masterfrom
zeeshanzzz788:fix/bogosort-fisher-yates-and-rgb-mutation

Conversation

@zeeshanzzz788

Copy link
Copy Markdown

Description

BogoSort (Sorts/BogoSort.js)
The previous shuffle used Math.random() * i and swapped with index i - 1, which is a biased shuffle (some permutations never occur). Replaced with standard Fisher–Yates over [0, i].

RgbHslConversion (Conversions/RgbHslConversion.js)
rgbToHsl aliased 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

const input = [24, 98, 118]
const result = rgbToHsl(input)
// input remains [24, 98, 118]; result is [193, 66, 28]; input !== result
bogoSort([3,1,2]) // -> [1,2,3]

Fixes #1867
Fixes #1907

- 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-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 (21221c4).

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.
📢 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

2 participants