From d17c7c50ba7dc2a5c1f1849a3ade9d5fb092b1ab Mon Sep 17 00:00:00 2001 From: Jc Calayag <138150245+Jccqt@users.noreply.github.com> Date: Mon, 14 Jul 2025 08:30:58 +0800 Subject: [PATCH 1/2] Add LINQ SHuffler algorithm --- .../Shufflers/LINQShufflerTests.cs | 60 +++++++++++++++++++ Algorithms/Shufflers/LINQShuffler.cs | 30 ++++++++++ 2 files changed, 90 insertions(+) create mode 100644 Algorithms.Tests/Shufflers/LINQShufflerTests.cs create mode 100644 Algorithms/Shufflers/LINQShuffler.cs diff --git a/Algorithms.Tests/Shufflers/LINQShufflerTests.cs b/Algorithms.Tests/Shufflers/LINQShufflerTests.cs new file mode 100644 index 00000000..c5e1d730 --- /dev/null +++ b/Algorithms.Tests/Shufflers/LINQShufflerTests.cs @@ -0,0 +1,60 @@ +using Algorithms.Shufflers; +using Algorithms.Tests.Helpers; +using FluentAssertions; +using NUnit.Framework; +using System; + +namespace Algorithms.Tests.Shufflers +{ + public static class LINQShufflerTests + { + [Test] + public static void ArrayShuffled_NewArraySameSize( + [Random(10, 1000, 100, Distinct = true)] + int n) + { + // Arrange + var shuffler = new LINQShuffler(); + var (correctArray, testArray) = RandomHelper.GetArrays(n); + + // Act + shuffler.Shuffle(testArray); + + // Assert + testArray.Length.Should().Be(correctArray.Length); + } + + [Test] + public static void ArrayShuffled_NewArraySameValues( + [Random(10, 1000, 100, Distinct = true)] + int n) + { + // Arrange + var shuffler = new LINQShuffler(); + var (correctArray, testArray) = RandomHelper.GetArrays(n); + + // Act + shuffler.Shuffle(testArray); + + // Assert + testArray.Should().BeEquivalentTo(correctArray); + } + + [Test] + public static void ArrayShuffled_NewArraySameShuffle( + [Random(0, 1000, 2, Distinct = true)] int n, + [Random(1000, 10000, 5, Distinct = true)] int seed) + { + // Arrange + var shuffle = new LINQShuffler(); + var (correctArray, testArray) = RandomHelper.GetArrays(n); + + // Act + shuffle.Shuffle(testArray, seed); + shuffle.Shuffle(correctArray, seed); + + // Assert + correctArray.Should().BeEquivalentTo(testArray, options => options.WithStrictOrdering()); + } + } +} diff --git a/Algorithms/Shufflers/LINQShuffler.cs b/Algorithms/Shufflers/LINQShuffler.cs new file mode 100644 index 00000000..6445519c --- /dev/null +++ b/Algorithms/Shufflers/LINQShuffler.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Algorithms.Shufflers +{ + /// + /// LINQ Shuffle is a simple shuffling algorithm, + /// where the elements within a collection are shuffled using + /// LINQ queries and lambda expressions in C#. + /// + /// Type array input. + public class LINQShuffler : IShuffler + { + /// + /// First, it will generate a random value for each element. + /// Next, it will sort the elements based on these generated + /// random numbers using OrderBy. + /// + /// Array to shuffle. + /// Random generator seed. Used to repeat the shuffle. + public void Shuffle(T[] array, int? seed = null) + { + var random = seed is null ? new Random() : new Random(seed.Value); + array = array.OrderBy(x => random.Next()).ToArray(); + } + } +} From 6fff006d471e0e873900c5821fc719822b3ab406 Mon Sep 17 00:00:00 2001 From: Jc Calayag <138150245+Jccqt@users.noreply.github.com> Date: Mon, 14 Jul 2025 08:38:43 +0800 Subject: [PATCH 2/2] Added LINQ Shuffler on readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b9933f9f..c5734067 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,7 @@ find more than one implementation for the same objective but using different alg * [MSD Radix Sort](./Algorithms/Sorters/String/MsdRadixStringSorter.cs) * [Shufflers](./Algorithms/Shufflers) * [Fisher-Yates Shuffler](./Algorithms/Shufflers/FisherYatesShuffler.cs) + * [LINQ Shuffler](./Algorithms/Shufflers/LINQShuffler.cs) * [Sequences](./Algorithms/Sequences) * [A000002 Kolakoski](./Algorithms/Sequences/KolakoskiSequence.cs) * [A000004 Zero](./Algorithms/Sequences/ZeroSequence.cs)