Skip to content

Commit eebbbfe

Browse files
Selection sort: move to .NET 10 and copy the benchmark input per call (#2369)
Both projects target net10.0 with BenchmarkDotNet 0.15.8, MSTest 4.5.2, Microsoft.NET.Test.Sdk 18.11.0 and coverlet.collector 10.1.0. The benchmark moves into SelectionBenchmarks, which copies its generated array into a work array on every call so the random case no longer sorts already sorted data after the first call. Program.cs passes args to BenchmarkRunner. Tests use (expected, actual) order and gain an Array.Sort oracle test and edge-case rows.
1 parent ac2c45f commit eebbbfe

6 files changed

Lines changed: 138 additions & 85 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
using BenchmarkDotNet.Running;
22
using SelectionSort;
33

4-
var summary = BenchmarkRunner.Run<Selection>();
4+
BenchmarkRunner.Run<SelectionBenchmarks>(args: args);
Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,51 @@
1-
using BenchmarkDotNet.Attributes;
1+
namespace SelectionSort;
22

3-
namespace SelectionSort
3+
public class Selection
44
{
5-
public class Selection
5+
public int[] NumArray { get; set; } = [];
6+
7+
public int[] SortArray()
68
{
7-
[ParamsSource(nameof(ArraySizes))]
8-
public int[]? NumArray { get; set; }
9-
int[] smallArray = AddRandomElements(200);
10-
int[] mediumArray = AddRandomElements(2000);
11-
int[] largeArray = AddRandomElements(200000);
12-
int[] sortedSmallArray = AddSortedElements(200);
13-
int[] sortedMediumArray = AddSortedElements(2000);
14-
int[] sortedLargeArray = AddSortedElements(200000);
15-
public IEnumerable<int[]> ArraySizes => new[] { sortedSmallArray, sortedMediumArray, sortedLargeArray };
16-
17-
[Benchmark]
18-
public int[] SortArray()
9+
var arrayLength = NumArray.Length;
10+
11+
for (int i = 0; i < arrayLength - 1; i++)
1912
{
20-
var arrayLength = NumArray.Length;
13+
var smallestVal = i;
2114

22-
for (int i = 0; i < arrayLength - 1; i++)
15+
for (int j = i + 1; j < arrayLength; j++)
2316
{
24-
var smallestVal = i;
25-
26-
for (int j = i + 1; j < arrayLength; j++)
17+
if (NumArray[j] < NumArray[smallestVal])
2718
{
28-
if (NumArray[j] < NumArray[smallestVal])
29-
{
30-
smallestVal = j;
31-
}
19+
smallestVal = j;
3220
}
33-
34-
var tempVar = NumArray[smallestVal];
35-
NumArray[smallestVal] = NumArray[i];
36-
NumArray[i] = tempVar;
3721
}
38-
return NumArray;
22+
23+
var tempVar = NumArray[smallestVal];
24+
NumArray[smallestVal] = NumArray[i];
25+
NumArray[i] = tempVar;
3926
}
27+
return NumArray;
28+
}
4029

41-
public static int[] AddRandomElements(int size)
42-
{
43-
var array = new int[size];
44-
var rand = new Random();
45-
var maxNum = 10000;
30+
public static int[] AddRandomElements(int size)
31+
{
32+
var array = new int[size];
33+
var rand = new Random();
34+
var maxNum = 10000;
4635

47-
for (int i = 0; i < size; i++)
48-
array[i] = rand.Next(maxNum + 1);
36+
for (int i = 0; i < size; i++)
37+
array[i] = rand.Next(maxNum + 1);
4938

50-
return array;
51-
}
39+
return array;
40+
}
5241

53-
public static int[] AddSortedElements(int size)
54-
{
55-
var array = new int[size];
42+
public static int[] AddSortedElements(int size)
43+
{
44+
var array = new int[size];
5645

57-
for (int i = 0; i < size; i++)
58-
array[i] = i;
46+
for (int i = 0; i < size; i++)
47+
array[i] = i;
5948

60-
return array;
61-
}
49+
return array;
6250
}
6351
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using BenchmarkDotNet.Attributes;
2+
3+
namespace SelectionSort;
4+
5+
public class SelectionBenchmarks
6+
{
7+
private readonly Selection _selection = new();
8+
private int[] _source = [];
9+
private int[] _work = [];
10+
11+
[Params(200, 2_000, 20_000)]
12+
public int Size { get; set; }
13+
14+
[Params("Random", "Sorted")]
15+
public string Input { get; set; } = "Random";
16+
17+
[GlobalSetup]
18+
public void Setup()
19+
{
20+
_source = Input == "Random"
21+
? Selection.AddRandomElements(Size)
22+
: Selection.AddSortedElements(Size);
23+
_work = new int[Size];
24+
}
25+
26+
[Benchmark]
27+
public int[] SortArray()
28+
{
29+
_source.CopyTo(_work, 0);
30+
_selection.NumArray = _work;
31+
32+
return _selection.SortArray();
33+
}
34+
}

‎csharp-algorithms/SelectionSort/SelectionSort/SelectionSort.csproj‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
11+
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
1212
</ItemGroup>
1313

1414
</Project>

‎csharp-algorithms/SelectionSort/SelectionSortTest/SelectionSortTest.csproj‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
56
<Nullable>enable</Nullable>
67

78
<IsPackable>false</IsPackable>
89
</PropertyGroup>
910

1011
<ItemGroup>
11-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
12-
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
13-
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
14-
<PackageReference Include="coverlet.collector" Version="3.1.0" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
13+
<PackageReference Include="MSTest.TestAdapter" Version="4.5.2" />
14+
<PackageReference Include="MSTest.TestFramework" Version="4.5.2" />
15+
<PackageReference Include="coverlet.collector" Version="10.1.0" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
1520
</ItemGroup>
1621

1722
<ItemGroup>
Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,71 @@
1-
using Microsoft.VisualStudio.TestTools.UnitTesting;
21
using SelectionSort;
32

4-
namespace SelectionSortTest
3+
namespace SelectionSortTest;
4+
5+
[TestClass]
6+
public class SelectionSortUnitTest
57
{
6-
[TestClass]
7-
public class SelectionSortUnitTest
8+
[TestMethod]
9+
public void GivenUnsortedArray_ThenReturnSortedArray()
810
{
9-
[TestMethod]
10-
public void GivenUnsortedArray_ThenReturnSortedArray()
11-
{
12-
var array = new int[] { 73, 57, 49, 99, 133, 20, 1 };
13-
var expected = new int[] { 1, 20, 49, 57, 73, 99, 133 };
14-
var sortFunction = new Selection();
15-
sortFunction.NumArray = array;
11+
var array = new int[] { 73, 57, 49, 99, 133, 20, 1 };
12+
var expected = new int[] { 1, 20, 49, 57, 73, 99, 133 };
13+
var sortFunction = new Selection();
14+
sortFunction.NumArray = array;
15+
16+
var sortedArray = sortFunction.SortArray();
17+
18+
Assert.IsNotNull(sortedArray);
19+
CollectionAssert.AreEqual(expected, sortedArray);
20+
}
1621

17-
var sortedArray = sortFunction.SortArray();
22+
[TestMethod]
23+
public void GivenUnsortedArray_CheckInstanceType()
24+
{
25+
var sortFunction = new Selection();
26+
sortFunction.NumArray = Selection.AddRandomElements(200);
1827

19-
Assert.IsNotNull(sortedArray);
20-
CollectionAssert.AreEqual(sortedArray, expected);
21-
}
28+
var sortedArray = sortFunction.SortArray();
2229

23-
[TestMethod]
24-
public void GivenUnsortedArray_CheckInstanceType()
25-
{
26-
var sortFunction = new Selection();
27-
sortFunction.NumArray = Selection.AddRandomElements(200);
30+
Assert.IsInstanceOfType(sortedArray, typeof(int[]));
31+
}
2832

29-
var sortedArray = sortFunction.SortArray();
33+
[TestMethod]
34+
public void GivenUnsortedArray_CheckSortedNotNull()
35+
{
36+
var sortFunction = new Selection();
37+
sortFunction.NumArray = Selection.AddRandomElements(200);
3038

31-
Assert.IsInstanceOfType(sortedArray, typeof(int[]));
32-
}
39+
var sortedArray = sortFunction.SortArray();
3340

34-
[TestMethod]
35-
public void GivenUnsortedArray_CheckSortedNotNull()
36-
{
37-
var sortFunction = new Selection();
38-
sortFunction.NumArray = Selection.AddRandomElements(200);
41+
Assert.IsNotNull(sortedArray);
42+
}
43+
44+
[TestMethod]
45+
public void GivenRandomArray_WhenSorted_ThenMatchesArraySort()
46+
{
47+
var source = Selection.AddRandomElements(500);
48+
var expected = source.ToArray();
49+
Array.Sort(expected);
50+
51+
var sortedArray = new Selection { NumArray = source.ToArray() }.SortArray();
52+
53+
CollectionAssert.AreEqual(expected, sortedArray);
54+
}
55+
56+
[TestMethod]
57+
[DataRow(new int[] { })]
58+
[DataRow(new int[] { 42 })]
59+
[DataRow(new int[] { 5, 3, 5, 1, 3, 5 })]
60+
[DataRow(new int[] { 1, 2, 3, 4, 5 })]
61+
[DataRow(new int[] { 5, 4, 3, 2, 1 })]
62+
public void GivenEdgeCaseArray_WhenSorted_ThenMatchesArraySort(int[] array)
63+
{
64+
var expected = array.ToArray();
65+
Array.Sort(expected);
3966

40-
var sortedArray = sortFunction.SortArray();
67+
var sortedArray = new Selection { NumArray = array }.SortArray();
4168

42-
Assert.IsNotNull(sortedArray);
43-
}
69+
CollectionAssert.AreEqual(expected, sortedArray);
4470
}
45-
}
71+
}

0 commit comments

Comments
 (0)