Skip to content
Merged
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
2 changes: 1 addition & 1 deletion csharp-algorithms/SelectionSort/SelectionSort/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using BenchmarkDotNet.Running;
using SelectionSort;

var summary = BenchmarkRunner.Run<Selection>();
BenchmarkRunner.Run<SelectionBenchmarks>(args: args);
78 changes: 33 additions & 45 deletions csharp-algorithms/SelectionSort/SelectionSort/Selection.cs
Original file line number Diff line number Diff line change
@@ -1,63 +1,51 @@
using BenchmarkDotNet.Attributes;
namespace SelectionSort;

namespace SelectionSort
public class Selection
{
public class Selection
public int[] NumArray { get; set; } = [];

public int[] SortArray()
{
[ParamsSource(nameof(ArraySizes))]
public int[]? NumArray { get; set; }
int[] smallArray = AddRandomElements(200);
int[] mediumArray = AddRandomElements(2000);
int[] largeArray = AddRandomElements(200000);
int[] sortedSmallArray = AddSortedElements(200);
int[] sortedMediumArray = AddSortedElements(2000);
int[] sortedLargeArray = AddSortedElements(200000);
public IEnumerable<int[]> ArraySizes => new[] { sortedSmallArray, sortedMediumArray, sortedLargeArray };

[Benchmark]
public int[] SortArray()
var arrayLength = NumArray.Length;

for (int i = 0; i < arrayLength - 1; i++)
{
var arrayLength = NumArray.Length;
var smallestVal = i;

for (int i = 0; i < arrayLength - 1; i++)
for (int j = i + 1; j < arrayLength; j++)
{
var smallestVal = i;

for (int j = i + 1; j < arrayLength; j++)
if (NumArray[j] < NumArray[smallestVal])
{
if (NumArray[j] < NumArray[smallestVal])
{
smallestVal = j;
}
smallestVal = j;
}

var tempVar = NumArray[smallestVal];
NumArray[smallestVal] = NumArray[i];
NumArray[i] = tempVar;
}
return NumArray;

var tempVar = NumArray[smallestVal];
NumArray[smallestVal] = NumArray[i];
NumArray[i] = tempVar;
}
return NumArray;
}

public static int[] AddRandomElements(int size)
{
var array = new int[size];
var rand = new Random();
var maxNum = 10000;
public static int[] AddRandomElements(int size)
{
var array = new int[size];
var rand = new Random();
var maxNum = 10000;

for (int i = 0; i < size; i++)
array[i] = rand.Next(maxNum + 1);
for (int i = 0; i < size; i++)
array[i] = rand.Next(maxNum + 1);

return array;
}
return array;
}

public static int[] AddSortedElements(int size)
{
var array = new int[size];
public static int[] AddSortedElements(int size)
{
var array = new int[size];

for (int i = 0; i < size; i++)
array[i] = i;
for (int i = 0; i < size; i++)
array[i] = i;

return array;
}
return array;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using BenchmarkDotNet.Attributes;

namespace SelectionSort;

public class SelectionBenchmarks
{
private readonly Selection _selection = new();
private int[] _source = [];
private int[] _work = [];

[Params(200, 2_000, 20_000)]
public int Size { get; set; }

[Params("Random", "Sorted")]
public string Input { get; set; } = "Random";

[GlobalSetup]
public void Setup()
{
_source = Input == "Random"
? Selection.AddRandomElements(Size)
: Selection.AddSortedElements(Size);
_work = new int[Size];
}

[Benchmark]
public int[] SortArray()
{
_source.CopyTo(_work, 0);
_selection.NumArray = _work;

return _selection.SortArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.5.2" />
<PackageReference Include="MSTest.TestFramework" Version="4.5.2" />
<PackageReference Include="coverlet.collector" Version="10.1.0" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,71 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SelectionSort;

namespace SelectionSortTest
namespace SelectionSortTest;

[TestClass]
public class SelectionSortUnitTest
{
[TestClass]
public class SelectionSortUnitTest
[TestMethod]
public void GivenUnsortedArray_ThenReturnSortedArray()
{
[TestMethod]
public void GivenUnsortedArray_ThenReturnSortedArray()
{
var array = new int[] { 73, 57, 49, 99, 133, 20, 1 };
var expected = new int[] { 1, 20, 49, 57, 73, 99, 133 };
var sortFunction = new Selection();
sortFunction.NumArray = array;
var array = new int[] { 73, 57, 49, 99, 133, 20, 1 };
var expected = new int[] { 1, 20, 49, 57, 73, 99, 133 };
var sortFunction = new Selection();
sortFunction.NumArray = array;

var sortedArray = sortFunction.SortArray();

Assert.IsNotNull(sortedArray);
CollectionAssert.AreEqual(expected, sortedArray);
}

var sortedArray = sortFunction.SortArray();
[TestMethod]
public void GivenUnsortedArray_CheckInstanceType()
{
var sortFunction = new Selection();
sortFunction.NumArray = Selection.AddRandomElements(200);

Assert.IsNotNull(sortedArray);
CollectionAssert.AreEqual(sortedArray, expected);
}
var sortedArray = sortFunction.SortArray();

[TestMethod]
public void GivenUnsortedArray_CheckInstanceType()
{
var sortFunction = new Selection();
sortFunction.NumArray = Selection.AddRandomElements(200);
Assert.IsInstanceOfType(sortedArray, typeof(int[]));
}

var sortedArray = sortFunction.SortArray();
[TestMethod]
public void GivenUnsortedArray_CheckSortedNotNull()
{
var sortFunction = new Selection();
sortFunction.NumArray = Selection.AddRandomElements(200);

Assert.IsInstanceOfType(sortedArray, typeof(int[]));
}
var sortedArray = sortFunction.SortArray();

[TestMethod]
public void GivenUnsortedArray_CheckSortedNotNull()
{
var sortFunction = new Selection();
sortFunction.NumArray = Selection.AddRandomElements(200);
Assert.IsNotNull(sortedArray);
}

[TestMethod]
public void GivenRandomArray_WhenSorted_ThenMatchesArraySort()
{
var source = Selection.AddRandomElements(500);
var expected = source.ToArray();
Array.Sort(expected);

var sortedArray = new Selection { NumArray = source.ToArray() }.SortArray();

CollectionAssert.AreEqual(expected, sortedArray);
}

[TestMethod]
[DataRow(new int[] { })]
[DataRow(new int[] { 42 })]
[DataRow(new int[] { 5, 3, 5, 1, 3, 5 })]
[DataRow(new int[] { 1, 2, 3, 4, 5 })]
[DataRow(new int[] { 5, 4, 3, 2, 1 })]
public void GivenEdgeCaseArray_WhenSorted_ThenMatchesArraySort(int[] array)
{
var expected = array.ToArray();
Array.Sort(expected);

var sortedArray = sortFunction.SortArray();
var sortedArray = new Selection { NumArray = array }.SortArray();

Assert.IsNotNull(sortedArray);
}
CollectionAssert.AreEqual(expected, sortedArray);
}
}
}
Loading