-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathRecursiveShuffler.cs
More file actions
43 lines (40 loc) · 1.65 KB
/
RecursiveShuffler.cs
File metadata and controls
43 lines (40 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
namespace Algorithms.Shufflers;
/// <summary>
/// Recursive Shuffler is a recursive version of
/// Fisher-Yates shuffle algorithm. This can only be used
/// for educational purposes due to stack depth limits.
/// </summary>
/// <typeparam name="T">Type array input.</typeparam>
public class RecursiveShuffler<T> : IShuffler<T>
{
/// <summary>
/// This is the public overload method that calls the private overload method.
/// </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)
{
Shuffle(array, array.Length - 1, seed);
}
/// <summary>
/// First, it will check the length of the array on the base case.
/// Next, if there's still items left, it will shuffle the sub-array.
/// Lastly, it will randomly select index from 0 to number of items of the array
/// then swap the elements array[items] and array[index].
/// </summary>
/// <param name="array">Array to shuffle.</param>
/// <param name="items">Number of items in the array.</param>
/// <param name="seed">Random generator seed. Used to repeat the shuffle.</param>
private void Shuffle(T[] array, int items, int? seed)
{
if (items <= 0)
{
return;
}
Shuffle(array, items - 1, seed);
var random = seed is null ? new Random() : new Random(seed.Value);
int index = random.Next(items + 1);
(array[items], array[index]) = (array[index], array[items]);
(array[items], array[index]) = (array[index], array[items]);
}
}