Skip to content

Commit 67abf36

Browse files
Dima1247DPalaSSsiriak
authored
Add Fisher-Yates Shuffle Algorithm (TheAlgorithms#281)
Co-authored-by: Dmytro Palamarchuk <dpala@softserveinc.com> Co-authored-by: Andrii Siriak <siryaka@gmail.com>
1 parent c4a2639 commit 67abf36

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 FisherYatesShufflerTests
10+
{
11+
[Test]
12+
public static void ArrayShuffled_NewArrayHasSameSize(
13+
[Random(0, 1000, 100, Distinct = true)]
14+
int n)
15+
{
16+
// Arrange
17+
var shuffler = new FisherYatesShuffler<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_NewArrayHasSameValues(
29+
[Random(0, 1000, 100, Distinct = true)]
30+
int n)
31+
{
32+
// Arrange
33+
var shuffler = new FisherYatesShuffler<int>();
34+
var (correctArray, testArray) = RandomHelper.GetArrays(n);
35+
36+
// Act
37+
shuffler.Shuffle(testArray);
38+
Array.Sort(testArray);
39+
Array.Sort(correctArray);
40+
41+
// Assert
42+
testArray.Should().BeEquivalentTo(correctArray);
43+
}
44+
}
45+
}

Algorithms/Algorithms.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
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+
/// Fisher-Yates shuffle is a simple shuffling algorithm,
7+
/// which is usually used to shuffle a deck of cards.
8+
/// </summary>
9+
/// <typeparam name="T">Type array input.</typeparam>
10+
public class FisherYatesShuffler<T> : IShuffler<T>
11+
{
12+
/// <summary>
13+
/// Shuffles input array using Fisher-Yates algorithm.
14+
/// The algorithm starts shuffling from the last element
15+
/// and swap elements one by one. We use random index to
16+
/// choose element we use in swap operation.
17+
/// </summary>
18+
/// <param name="array">Array to shuffle.</param>
19+
public void Shuffle(T[] array)
20+
{
21+
var random = new Random();
22+
23+
for (var i = array.Length - 1; i > 0; i--)
24+
{
25+
var j = random.Next(0, i + 1);
26+
27+
(array[i], array[j]) = (array[j], array[i]);
28+
}
29+
}
30+
}
31+
}

Algorithms/Shufflers/IShuffler.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Algorithms.Shufflers
2+
{
3+
/// <summary>
4+
/// Shuffles array.
5+
/// </summary>
6+
/// <typeparam name="T">Type of array item.</typeparam>
7+
public interface IShuffler<in T>
8+
{
9+
/// <summary>
10+
/// Shuffles array.
11+
/// </summary>
12+
/// <param name="array">Array to Shuffle.</param>
13+
void Shuffle(T[] array);
14+
}
15+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ This repository contains algorithms and data structures implemented in C# for ed
7070
* [Cocktail Sort](./Algorithms/Sorters/Comparison/CocktailSorter.cs)
7171
* [Comb Sort](./Algorithms/Sorters/Comparison/CombSorter.cs)
7272
* [Cycle Sort](./Algorithms/Sorters/Comparison/CycleSorter.cs)
73+
* [Fisher-Yates Shuffle](./Algorithms/Sorters/Comparison/FisherYatesShuffler.cs)
7374
* [Heap Sort](./Algorithms/Sorters/Comparison/HeapSorter.cs)
7475
* [Insertion Sort](./Algorithms/Sorters/Comparison/InsertionSorter.cs)
7576
* [Merge Sort](./Algorithms/Sorters/Comparison/MergeSorter.cs)

0 commit comments

Comments
 (0)