Skip to content

Add Naive shuffling algorithm #516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Algorithms.Tests/Shufflers/NaiveShufflerTests.cs
Original file line number Diff line number Diff line change
@@ -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<int>();
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<int>();
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<int>();
var (correctArray, testArray) = RandomHelper.GetArrays(n);

// Act
shuffle.Shuffle(testArray, seed);
shuffle.Shuffle(correctArray, seed);

// Assert
correctArray.Should().BeEquivalentTo(testArray, options => options.WithStrictOrdering());
}
}
}
31 changes: 31 additions & 0 deletions Algorithms/Shufflers/NaiveShuffler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;

namespace Algorithms.Shufflers
{
/// <summary>
/// Naive Shuffle is a simple and incorrect shuffling algorithm
/// that randomly swaps every element with any other element in the array.
/// </summary>
/// <typeparam name="T">Type array input.</typeparam>
public class NaiveShuffler<T> : IShuffler<T>
{
/// <summary>
/// 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].
/// </summary>
/// <param name="array">Array to shuffle.</param>
/// <param name="seed">Random generator seed. Used to repeat the shuffle.</param>
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;
}
}
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down