From ebb998ea630dbbc27e3e3d6e0f8bf1757afefff7 Mon Sep 17 00:00:00 2001 From: Jc Calayag <138150245+Jccqt@users.noreply.github.com> Date: Thu, 17 Jul 2025 17:30:51 +0800 Subject: [PATCH] Add Naive shuffling algorithm --- .../Shufflers/NaiveShufflerTests.cs | 60 +++++++++++++++++++ Algorithms/Shufflers/NaiveShuffler.cs | 31 ++++++++++ README.md | 1 + 3 files changed, 92 insertions(+) create mode 100644 Algorithms.Tests/Shufflers/NaiveShufflerTests.cs create mode 100644 Algorithms/Shufflers/NaiveShuffler.cs diff --git a/Algorithms.Tests/Shufflers/NaiveShufflerTests.cs b/Algorithms.Tests/Shufflers/NaiveShufflerTests.cs new file mode 100644 index 00000000..9fc9ba00 --- /dev/null +++ b/Algorithms.Tests/Shufflers/NaiveShufflerTests.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 NaiveShufflerTests + { + [Test] + public static void ArrayShuffled_NewArraySameSize( + [Random(10, 1000, 100, Distinct = true)] + int n) + { + // Arrange + var shuffler = new NaiveShuffler(); + 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 NaiveShuffler(); + 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/NaiveShuffler.cs b/Algorithms/Shufflers/NaiveShuffler.cs new file mode 100644 index 00000000..94e10d18 --- /dev/null +++ b/Algorithms/Shufflers/NaiveShuffler.cs @@ -0,0 +1,31 @@ +using System; + +namespace Algorithms.Shufflers +{ + /// + /// Naive Shuffle is a simple and incorrect shuffling algorithm + /// that randomly swaps every element with any other element in the array. + /// + /// Type array input. + public class NaiveShuffler : IShuffler + { + /// + /// First, it loop from 0 to n - 1. + /// Next, it will randomly pick any j in the array. + /// Lastly, it will swap array[i] with array[j]. + /// + /// 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); + for(int i = 0; i < array.Length; i++) + { + int j = random.Next(array.Length); + T temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } + } + } +} diff --git a/README.md b/README.md index 9f1fef06..bd8490d3 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,7 @@ find more than one implementation for the same objective but using different alg * [Shufflers](./Algorithms/Shufflers) * [Fisher-Yates Shuffler](./Algorithms/Shufflers/FisherYatesShuffler.cs) * [LINQ Shuffler](./Algorithms/Shufflers/LinqShuffler.cs) + * [Naive Shuffler](./Algorithms/Shufflers/NaiveShuffler.cs) * [Sequences](./Algorithms/Sequences) * [A000002 Kolakoski](./Algorithms/Sequences/KolakoskiSequence.cs) * [A000004 Zero](./Algorithms/Sequences/ZeroSequence.cs)