Skip to content

Commit 3c13dae

Browse files
Heap Sort: .NET 10, copy-per-call benchmark, stronger tests (#2376)
Move both projects to 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 HeapSortBenchmarks, which copies the generated input into a work array on every call, so the random case no longer sorts an already sorted array after the first call. SortArray takes (array, size) as the article prints it. Tests use (expected, actual) order and cover the article example, empty, sorted, identical and random input checked against Array.Sort.
1 parent c085825 commit 3c13dae

6 files changed

Lines changed: 178 additions & 119 deletions

File tree

‎csharp-algorithms/HeapSort/HeapSort/HeapSort.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>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using BenchmarkDotNet.Attributes;
2+
3+
namespace HeapSort;
4+
5+
public class HeapSortBenchmarks
6+
{
7+
private const int Size = 2_000_000;
8+
9+
private readonly HeapSortMethods _heapSort = new();
10+
private int[] _source = [];
11+
private int[] _work = [];
12+
13+
[Params("Random", "Sorted", "Identical")]
14+
public string Input { get; set; } = "Random";
15+
16+
[GlobalSetup]
17+
public void Setup()
18+
{
19+
_source = Input switch
20+
{
21+
"Sorted" => HeapSortMethods.CreateSortedArray(Size),
22+
"Identical" => HeapSortMethods.CreateIdenticalArray(Size, 1, Size),
23+
_ => HeapSortMethods.CreateRandomArray(Size, 1, Size)
24+
};
25+
_work = new int[Size];
26+
}
27+
28+
[Benchmark]
29+
public int[] SortArray()
30+
{
31+
_source.CopyTo(_work, 0);
32+
33+
return _heapSort.SortArray(_work, Size);
34+
}
35+
}
Lines changed: 59 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,88 @@
1-
using BenchmarkDotNet.Attributes;
1+
namespace HeapSort;
22

3-
namespace HeapSort
3+
public class HeapSortMethods
44
{
5-
public class HeapSortMethods
5+
public int[] SortArray(int[] array, int size)
66
{
7-
public IEnumerable<object[]> SampleArrays()
7+
if (size <= 1)
8+
return array;
9+
10+
for (int i = size / 2 - 1; i >= 0; i--)
811
{
9-
yield return new object[] { CreateSortedArray(2000000), 2000000, "Worst Case" };
10-
yield return new object[] { CreateRandomArray(2000000, 1, 2000000), 2000000, "Average Case" };
11-
yield return new object[] { CreateIdenticalArray(2000000, 1, 2000000), 2000000, "Best Case" };
12+
Heapify(array, size, i);
1213
}
1314

14-
[Benchmark]
15-
[ArgumentsSource(nameof(SampleArrays))]
16-
public int[] SortArray(int[] array, int size, string arrayName)
15+
for (int i = size - 1; i >= 0; i--)
1716
{
18-
if (size <= 1)
19-
return array;
17+
var tempVar = array[0];
18+
array[0] = array[i];
19+
array[i] = tempVar;
2020

21-
for (int i = size / 2 - 1; i >= 0; i--)
22-
{
23-
Heapify(array, size, i);
24-
}
21+
Heapify(array, i, 0);
22+
}
2523

26-
for (int i = size - 1; i >= 0; i--)
27-
{
28-
var tempVar = array[0];
29-
array[0] = array[i];
30-
array[i] = tempVar;
24+
return array;
25+
}
3126

32-
Heapify(array, i, 0);
33-
}
27+
static void Heapify(int[] array, int size, int index)
28+
{
29+
var largestIndex = index;
30+
var leftChild = 2 * index + 1;
31+
var rightChild = 2 * index + 2;
3432

35-
return array;
33+
if (leftChild < size && array[leftChild] > array[largestIndex])
34+
{
35+
largestIndex = leftChild;
3636
}
3737

38-
static void Heapify(int[] array, int size, int index)
38+
if (rightChild < size && array[rightChild] > array[largestIndex])
3939
{
40-
var largestIndex = index;
41-
var leftChild = 2 * index + 1;
42-
var rightChild = 2 * index + 2;
43-
44-
if (leftChild < size && array[leftChild] > array[largestIndex])
45-
{
46-
largestIndex = leftChild;
47-
}
48-
49-
if (rightChild < size && array[rightChild] > array[largestIndex])
50-
{
51-
largestIndex = rightChild;
52-
}
53-
54-
if (largestIndex != index)
55-
{
56-
var tempVar = array[index];
57-
array[index] = array[largestIndex];
58-
array[largestIndex] = tempVar;
59-
60-
Heapify(array, size, largestIndex);
61-
}
40+
largestIndex = rightChild;
6241
}
6342

64-
public static int[] CreateRandomArray(int size, int lower, int upper)
43+
if (largestIndex != index)
6544
{
66-
var array = new int[size];
67-
var rand = new Random();
68-
69-
for (int i = 0; i < size; i++)
70-
array[i] = rand.Next(lower, upper);
45+
var tempVar = array[index];
46+
array[index] = array[largestIndex];
47+
array[largestIndex] = tempVar;
7148

72-
return array;
49+
Heapify(array, size, largestIndex);
7350
}
51+
}
7452

75-
public static int[] CreateSortedArray(int size)
76-
{
77-
var array = new int[size];
53+
public static int[] CreateRandomArray(int size, int lower, int upper)
54+
{
55+
var array = new int[size];
56+
var rand = new Random();
7857

79-
for (int i = 0; i < size; i++)
80-
array[i] = i;
58+
for (int i = 0; i < size; i++)
59+
array[i] = rand.Next(lower, upper);
8160

82-
return array;
83-
}
61+
return array;
62+
}
8463

85-
public static int[] CreateIdenticalArray(int size, int lower, int upper)
86-
{
87-
var array = new int[size];
88-
var rand = new Random();
64+
public static int[] CreateSortedArray(int size)
65+
{
66+
var array = new int[size];
67+
68+
for (int i = 0; i < size; i++)
69+
array[i] = i;
8970

90-
var selectedElement = rand.Next(lower, upper);
71+
return array;
72+
}
9173

92-
for (int i = 0; i < size; i++)
93-
{
94-
array[i] = selectedElement;
95-
}
74+
public static int[] CreateIdenticalArray(int size, int lower, int upper)
75+
{
76+
var array = new int[size];
77+
var rand = new Random();
9678

97-
return array;
79+
var selectedElement = rand.Next(lower, upper);
80+
81+
for (int i = 0; i < size; i++)
82+
{
83+
array[i] = selectedElement;
9884
}
85+
86+
return array;
9987
}
10088
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
using BenchmarkDotNet.Running;
22
using HeapSort;
33

4-
var summary = BenchmarkRunner.Run<HeapSortMethods>();
4+
BenchmarkRunner.Run<HeapSortBenchmarks>(args: args);

‎csharp-algorithms/HeapSort/HeapSortTests/HeapSortTests.csproj‎

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

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77

88
<IsPackable>false</IsPackable>
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
13-
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
14-
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
15-
<PackageReference Include="coverlet.collector" Version="3.1.2" />
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" />
1616
</ItemGroup>
1717

1818
<ItemGroup>
Lines changed: 76 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,93 @@
11
using HeapSort;
22

3-
namespace HeapSortTests
3+
namespace HeapSortTests;
4+
5+
[TestClass]
6+
public class HeapSortUnitTests
47
{
5-
[TestClass]
6-
public class HeapSortUnitTests
8+
[TestMethod]
9+
public void GivenUnsortedArray_WhenArrayIsNotEmpty_ThenReturnSortedArray()
710
{
8-
[TestMethod]
9-
public void GivenUnsortedArray_WhenArrayIsNotEmpty_ThenReturnSortedArray()
10-
{
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 HeapSortMethods();
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 HeapSortMethods();
1414

15-
var sortedArray = sortFunction.SortArray(array, array.Length, string.Empty);
15+
var sortedArray = sortFunction.SortArray(array, array.Length);
1616

17-
Assert.IsNotNull(sortedArray);
18-
CollectionAssert.AreEqual(sortedArray, expected);
19-
}
17+
Assert.IsNotNull(sortedArray);
18+
CollectionAssert.AreEqual(expected, sortedArray);
19+
}
2020

21-
[TestMethod]
22-
public void GivenUnsortedArray_WhenArrayIsRandomized_ThenCheckInstanceType()
23-
{
24-
var sortFunction = new HeapSortMethods();
25-
var array = HeapSortMethods.CreateRandomArray(200, 0, 200);
21+
[TestMethod]
22+
public void GivenArticleExample_WhenSorted_ThenNegativeValueComesFirst()
23+
{
24+
var array = new int[] { 12, 2, 24, 51, 8, -5 };
25+
var expected = new int[] { -5, 2, 8, 12, 24, 51 };
26+
var sortFunction = new HeapSortMethods();
2627

27-
var sortedArray = sortFunction.SortArray(array, array.Length, string.Empty);
28+
var sortedArray = sortFunction.SortArray(array, array.Length);
2829

29-
Assert.IsInstanceOfType(sortedArray, typeof(int[]));
30-
}
30+
CollectionAssert.AreEqual(expected, sortedArray);
31+
}
3132

32-
[TestMethod]
33-
public void GivenUnsortedArray_WhenArrayIsSorted_ThenResultingArrayIsNotNull()
34-
{
35-
var sortFunction = new HeapSortMethods(); ;
36-
var array = HeapSortMethods.CreateRandomArray(200, 0, 200);
33+
[TestMethod]
34+
public void GivenArray_WhenArrayHasSingleElement_ThenReturnSortedArray()
35+
{
36+
var array = new int[] { 73 };
37+
var expected = new int[] { 73 };
38+
var sortFunction = new HeapSortMethods();
3739

38-
var sortedArray = sortFunction.SortArray(array, array.Length, string.Empty);
40+
var sortedArray = sortFunction.SortArray(array, array.Length);
3941

40-
Assert.IsNotNull(sortedArray);
41-
}
42+
Assert.IsNotNull(sortedArray);
43+
CollectionAssert.AreEqual(expected, sortedArray);
44+
}
4245

43-
[TestMethod]
44-
public void GivenArray_WhenArrayHasSingleElement_ThenReturnSortedArray()
45-
{
46-
var array = new int[] { 73 };
47-
var expected = new int[] { 73 };
48-
var sortFunction = new HeapSortMethods();
46+
[TestMethod]
47+
public void GivenEmptyArray_WhenSorted_ThenReturnEmptyArray()
48+
{
49+
var array = Array.Empty<int>();
50+
var sortFunction = new HeapSortMethods();
51+
52+
var sortedArray = sortFunction.SortArray(array, array.Length);
4953

50-
var sortedArray = sortFunction.SortArray(array, array.Length, string.Empty);
54+
Assert.AreEqual(0, sortedArray.Length);
55+
}
56+
57+
[TestMethod]
58+
public void GivenSortedArray_WhenSorted_ThenArrayStaysSorted()
59+
{
60+
var array = HeapSortMethods.CreateSortedArray(1_000);
61+
var expected = HeapSortMethods.CreateSortedArray(1_000);
62+
var sortFunction = new HeapSortMethods();
63+
64+
var sortedArray = sortFunction.SortArray(array, array.Length);
65+
66+
CollectionAssert.AreEqual(expected, sortedArray);
67+
}
68+
69+
[TestMethod]
70+
public void GivenIdenticalValues_WhenSorted_ThenValuesAreUnchanged()
71+
{
72+
var array = HeapSortMethods.CreateIdenticalArray(1_000, 1, 100);
73+
var expected = (int[])array.Clone();
74+
var sortFunction = new HeapSortMethods();
75+
76+
var sortedArray = sortFunction.SortArray(array, array.Length);
77+
78+
CollectionAssert.AreEqual(expected, sortedArray);
79+
}
80+
81+
[TestMethod]
82+
public void GivenRandomArrayWithDuplicates_WhenSorted_ThenMatchesArraySort()
83+
{
84+
var array = HeapSortMethods.CreateRandomArray(5_000, 0, 500);
85+
var expected = (int[])array.Clone();
86+
Array.Sort(expected);
87+
var sortFunction = new HeapSortMethods();
5188

52-
Assert.IsNotNull(sortedArray);
53-
CollectionAssert.AreEqual(sortedArray, expected);
54-
}
89+
var sortedArray = sortFunction.SortArray(array, array.Length);
5590

91+
CollectionAssert.AreEqual(expected, sortedArray);
5692
}
57-
}
93+
}

0 commit comments

Comments
 (0)