Skip to content

Commit 99bc144

Browse files
authored
Add Naive shuffling algorithm (#517)
1 parent b024499 commit 99bc144

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using Algorithms.Shufflers;
2+
using Algorithms.Tests.Helpers;
3+
using FluentAssertions;
4+
using NUnit.Framework;
5+
using System;
6+
7+
namespace Algorithms.Tests.Shufflers
8+
{
9+
public static class NaiveShufflerTests
10+
{
11+
[Test]
12+
public static void ArrayShuffled_NewArraySameSize(
13+
[Random(10, 1000, 100, Distinct = true)]
14+
int n)
15+
{
16+
// Arrange
17+
var shuffler = new NaiveShuffler<int>();
18+
var (correctArray, testArray) = RandomHelper.GetArrays(n);
19+
20+
// Act
21+
shuffler.Shuffle(testArray);
22+
23+
// Assert
24+
testArray.Length.Should().Be(correctArray.Length);
25+
}
26+
27+
[Test]
28+
public static void ArrayShuffled_NewArraySameValues(
29+
[Random(10, 1000, 100, Distinct = true)]
30+
int n)
31+
{
32+
// Arrange
33+
var shuffler = new NaiveShuffler<int>();
34+
var (correctArray, testArray) = RandomHelper.GetArrays(n);
35+
36+
// Act
37+
shuffler.Shuffle(testArray);
38+
39+
// Assert
40+
testArray.Should().BeEquivalentTo(correctArray);
41+
}
42+
43+
[Test]
44+
public static void ArrayShuffled_NewArraySameShuffle(
45+
[Random(0, 1000, 2, Distinct = true)] int n,
46+
[Random(1000, 10000, 5, Distinct = true)] int seed)
47+
{
48+
// Arrange
49+
var shuffle = new NaiveShuffler<int>();
50+
var (correctArray, testArray) = RandomHelper.GetArrays(n);
51+
52+
// Act
53+
shuffle.Shuffle(testArray, seed);
54+
shuffle.Shuffle(correctArray, seed);
55+
56+
// Assert
57+
correctArray.Should().BeEquivalentTo(testArray, options => options.WithStrictOrdering());
58+
}
59+
}
60+
}

Algorithms/Shufflers/NaiveShuffler.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
3+
namespace Algorithms.Shufflers
4+
{
5+
/// <summary>
6+
/// Naive Shuffle is a simple and incorrect shuffling algorithm
7+
/// that randomly swaps every element with any other element in the array.
8+
/// </summary>
9+
/// <typeparam name="T">Type array input.</typeparam>
10+
public class NaiveShuffler<T> : IShuffler<T>
11+
{
12+
/// <summary>
13+
/// First, it loop from 0 to n - 1.
14+
/// Next, it will randomly pick any j in the array.
15+
/// Lastly, it will swap array[i] with array[j].
16+
/// </summary>
17+
/// <param name="array">Array to shuffle.</param>
18+
/// <param name="seed">Random generator seed. Used to repeat the shuffle.</param>
19+
public void Shuffle(T[] array, int? seed = null)
20+
{
21+
var random = seed is null ? new Random() : new Random(seed.Value);
22+
for(int i = 0; i < array.Length; i++)
23+
{
24+
int j = random.Next(array.Length);
25+
T temp = array[i];
26+
array[i] = array[j];
27+
array[j] = temp;
28+
}
29+
}
30+
}
31+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ find more than one implementation for the same objective but using different alg
149149
* [Shufflers](./Algorithms/Shufflers)
150150
* [Fisher-Yates Shuffler](./Algorithms/Shufflers/FisherYatesShuffler.cs)
151151
* [LINQ Shuffler](./Algorithms/Shufflers/LinqShuffler.cs)
152+
* [Naive Shuffler](./Algorithms/Shufflers/NaiveShuffler.cs)
152153
* [Sequences](./Algorithms/Sequences)
153154
* [A000002 Kolakoski](./Algorithms/Sequences/KolakoskiSequence.cs)
154155
* [A000004 Zero](./Algorithms/Sequences/ZeroSequence.cs)

0 commit comments

Comments
 (0)