Skip to content
Open
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
4 changes: 2 additions & 2 deletions csharp-algorithms/HeapSort/HeapSort/HeapSort.csproj
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>
35 changes: 35 additions & 0 deletions csharp-algorithms/HeapSort/HeapSort/HeapSortBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using BenchmarkDotNet.Attributes;

namespace HeapSort;

public class HeapSortBenchmarks
{
private const int Size = 2_000_000;

private readonly HeapSortMethods _heapSort = new();
private int[] _source = [];
private int[] _work = [];

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

[GlobalSetup]
public void Setup()
{
_source = Input switch
{
"Sorted" => HeapSortMethods.CreateSortedArray(Size),
"Identical" => HeapSortMethods.CreateIdenticalArray(Size, 1, Size),
_ => HeapSortMethods.CreateRandomArray(Size, 1, Size)
};
_work = new int[Size];
}

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

return _heapSort.SortArray(_work, Size);
}
}
130 changes: 59 additions & 71 deletions csharp-algorithms/HeapSort/HeapSort/HeapSortMethods.cs
Original file line number Diff line number Diff line change
@@ -1,100 +1,88 @@
using BenchmarkDotNet.Attributes;
namespace HeapSort;

namespace HeapSort
public class HeapSortMethods
{
public class HeapSortMethods
public int[] SortArray(int[] array, int size)
{
public IEnumerable<object[]> SampleArrays()
if (size <= 1)
return array;

for (int i = size / 2 - 1; i >= 0; i--)
{
yield return new object[] { CreateSortedArray(2000000), 2000000, "Worst Case" };
yield return new object[] { CreateRandomArray(2000000, 1, 2000000), 2000000, "Average Case" };
yield return new object[] { CreateIdenticalArray(2000000, 1, 2000000), 2000000, "Best Case" };
Heapify(array, size, i);
}

[Benchmark]
[ArgumentsSource(nameof(SampleArrays))]
public int[] SortArray(int[] array, int size, string arrayName)
for (int i = size - 1; i >= 0; i--)
{
if (size <= 1)
return array;
var tempVar = array[0];
array[0] = array[i];
array[i] = tempVar;

for (int i = size / 2 - 1; i >= 0; i--)
{
Heapify(array, size, i);
}
Heapify(array, i, 0);
}

for (int i = size - 1; i >= 0; i--)
{
var tempVar = array[0];
array[0] = array[i];
array[i] = tempVar;
return array;
}

Heapify(array, i, 0);
}
static void Heapify(int[] array, int size, int index)
{
var largestIndex = index;
var leftChild = 2 * index + 1;
var rightChild = 2 * index + 2;

return array;
if (leftChild < size && array[leftChild] > array[largestIndex])
{
largestIndex = leftChild;
}

static void Heapify(int[] array, int size, int index)
if (rightChild < size && array[rightChild] > array[largestIndex])
{
var largestIndex = index;
var leftChild = 2 * index + 1;
var rightChild = 2 * index + 2;

if (leftChild < size && array[leftChild] > array[largestIndex])
{
largestIndex = leftChild;
}

if (rightChild < size && array[rightChild] > array[largestIndex])
{
largestIndex = rightChild;
}

if (largestIndex != index)
{
var tempVar = array[index];
array[index] = array[largestIndex];
array[largestIndex] = tempVar;

Heapify(array, size, largestIndex);
}
largestIndex = rightChild;
}

public static int[] CreateRandomArray(int size, int lower, int upper)
if (largestIndex != index)
{
var array = new int[size];
var rand = new Random();

for (int i = 0; i < size; i++)
array[i] = rand.Next(lower, upper);
var tempVar = array[index];
array[index] = array[largestIndex];
array[largestIndex] = tempVar;

return array;
Heapify(array, size, largestIndex);
}
}

public static int[] CreateSortedArray(int size)
{
var array = new int[size];
public static int[] CreateRandomArray(int size, int lower, int upper)
{
var array = new int[size];
var rand = new Random();

for (int i = 0; i < size; i++)
array[i] = i;
for (int i = 0; i < size; i++)
array[i] = rand.Next(lower, upper);

return array;
}
return array;
}

public static int[] CreateIdenticalArray(int size, int lower, int upper)
{
var array = new int[size];
var rand = new Random();
public static int[] CreateSortedArray(int size)
{
var array = new int[size];

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

var selectedElement = rand.Next(lower, upper);
return array;
}

for (int i = 0; i < size; i++)
{
array[i] = selectedElement;
}
public static int[] CreateIdenticalArray(int size, int lower, int upper)
{
var array = new int[size];
var rand = new Random();

return array;
var selectedElement = rand.Next(lower, upper);

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

return array;
}
}
2 changes: 1 addition & 1 deletion csharp-algorithms/HeapSort/HeapSort/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using BenchmarkDotNet.Running;
using HeapSort;

var summary = BenchmarkRunner.Run<HeapSortMethods>();
BenchmarkRunner.Run<HeapSortBenchmarks>(args: args);
10 changes: 5 additions & 5 deletions csharp-algorithms/HeapSort/HeapSortTests/HeapSortTests.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<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="17.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<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>
Expand Down
116 changes: 76 additions & 40 deletions csharp-algorithms/HeapSort/HeapSortTests/HeapSortUnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,57 +1,93 @@
using HeapSort;

namespace HeapSortTests
namespace HeapSortTests;

[TestClass]
public class HeapSortUnitTests
{
[TestClass]
public class HeapSortUnitTests
[TestMethod]
public void GivenUnsortedArray_WhenArrayIsNotEmpty_ThenReturnSortedArray()
{
[TestMethod]
public void GivenUnsortedArray_WhenArrayIsNotEmpty_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 HeapSortMethods();
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 HeapSortMethods();

var sortedArray = sortFunction.SortArray(array, array.Length, string.Empty);
var sortedArray = sortFunction.SortArray(array, array.Length);

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

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

var sortedArray = sortFunction.SortArray(array, array.Length, string.Empty);
var sortedArray = sortFunction.SortArray(array, array.Length);

Assert.IsInstanceOfType(sortedArray, typeof(int[]));
}
CollectionAssert.AreEqual(expected, sortedArray);
}

[TestMethod]
public void GivenUnsortedArray_WhenArrayIsSorted_ThenResultingArrayIsNotNull()
{
var sortFunction = new HeapSortMethods(); ;
var array = HeapSortMethods.CreateRandomArray(200, 0, 200);
[TestMethod]
public void GivenArray_WhenArrayHasSingleElement_ThenReturnSortedArray()
{
var array = new int[] { 73 };
var expected = new int[] { 73 };
var sortFunction = new HeapSortMethods();

var sortedArray = sortFunction.SortArray(array, array.Length, string.Empty);
var sortedArray = sortFunction.SortArray(array, array.Length);

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

[TestMethod]
public void GivenArray_WhenArrayHasSingleElement_ThenReturnSortedArray()
{
var array = new int[] { 73 };
var expected = new int[] { 73 };
var sortFunction = new HeapSortMethods();
[TestMethod]
public void GivenEmptyArray_WhenSorted_ThenReturnEmptyArray()
{
var array = Array.Empty<int>();
var sortFunction = new HeapSortMethods();

var sortedArray = sortFunction.SortArray(array, array.Length);

var sortedArray = sortFunction.SortArray(array, array.Length, string.Empty);
Assert.AreEqual(0, sortedArray.Length);
}

[TestMethod]
public void GivenSortedArray_WhenSorted_ThenArrayStaysSorted()
{
var array = HeapSortMethods.CreateSortedArray(1_000);
var expected = HeapSortMethods.CreateSortedArray(1_000);
var sortFunction = new HeapSortMethods();

var sortedArray = sortFunction.SortArray(array, array.Length);

CollectionAssert.AreEqual(expected, sortedArray);
}

[TestMethod]
public void GivenIdenticalValues_WhenSorted_ThenValuesAreUnchanged()
{
var array = HeapSortMethods.CreateIdenticalArray(1_000, 1, 100);
var expected = (int[])array.Clone();
var sortFunction = new HeapSortMethods();

var sortedArray = sortFunction.SortArray(array, array.Length);

CollectionAssert.AreEqual(expected, sortedArray);
}

[TestMethod]
public void GivenRandomArrayWithDuplicates_WhenSorted_ThenMatchesArraySort()
{
var array = HeapSortMethods.CreateRandomArray(5_000, 0, 500);
var expected = (int[])array.Clone();
Array.Sort(expected);
var sortFunction = new HeapSortMethods();

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

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