Skip to content

Commit b024499

Browse files
authored
Add LINQ shuffling algorithm (#515)
1 parent 8fec042 commit b024499

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-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 LinqShufflerTests
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 LinqShuffler<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 LinqShuffler<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 LinqShuffler<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/LINQShuffler.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Algorithms.Shufflers
8+
{
9+
/// <summary>
10+
/// LINQ Shuffle is a simple shuffling algorithm,
11+
/// where the elements within a collection are shuffled using
12+
/// LINQ queries and lambda expressions in C#.
13+
/// </summary>
14+
/// <typeparam name="T">Type array input.</typeparam>
15+
public class LinqShuffler<T>
16+
{
17+
/// <summary>
18+
/// First, it will generate a random value for each element.
19+
/// Next, it will sort the elements based on these generated
20+
/// random numbers using OrderBy.
21+
/// </summary>
22+
/// <param name="array">Array to shuffle.</param>
23+
/// <param name="seed">Random generator seed. Used to repeat the shuffle.</param>
24+
public T[] Shuffle(T[] array, int? seed = null)
25+
{
26+
var random = seed is null ? new Random() : new Random(seed.Value);
27+
return array.OrderBy(x => random.Next()).ToArray();
28+
}
29+
}
30+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ find more than one implementation for the same objective but using different alg
148148
* [MSD Radix Sort](./Algorithms/Sorters/String/MsdRadixStringSorter.cs)
149149
* [Shufflers](./Algorithms/Shufflers)
150150
* [Fisher-Yates Shuffler](./Algorithms/Shufflers/FisherYatesShuffler.cs)
151+
* [LINQ Shuffler](./Algorithms/Shufflers/LinqShuffler.cs)
151152
* [Sequences](./Algorithms/Sequences)
152153
* [A000002 Kolakoski](./Algorithms/Sequences/KolakoskiSequence.cs)
153154
* [A000004 Zero](./Algorithms/Sequences/ZeroSequence.cs)

0 commit comments

Comments
 (0)