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
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
@@ -0,0 +1,43 @@
using BenchmarkDotNet.Attributes;

namespace CountingSort;

public class CountingSortBenchmarks
{
private readonly CountingSortMethods _sorter = new();
private int[] _source = [];
private int[] _work = [];

[Params("Best Case", "Average Case", "Worst Case")]
public string Input { get; set; } = "Average Case";

[GlobalSetup]
public void Setup()
{
_source = Input switch
{
"Best Case" => CountingSortMethods.CreateRandomArray(20_000, 1, 2),
"Average Case" => CountingSortMethods.CreateRandomArray(20_000, 10_000, 30_000),
_ => CountingSortMethods.CreateImbalancedArray(
CountingSortMethods.CreateRandomArray(19_999, 10_000, 30_000))
};
_work = new int[_source.Length];
}

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

return _sorter.CountingSort(_work);
}

[Benchmark(Baseline = true)]
public int[] ArraySort()
{
_source.CopyTo(_work, 0);
Array.Sort(_work);

return _work;
}
}
115 changes: 61 additions & 54 deletions csharp-algorithms/CountingSort/CountingSort/CountingSortMethods.cs
Original file line number Diff line number Diff line change
@@ -1,79 +1,86 @@
using BenchmarkDotNet.Attributes;
namespace CountingSort;

namespace CountingSort
public class CountingSortMethods
{
public class CountingSortMethods
public int[] CountingSort(int[] array)
{
public IEnumerable<object[]> SampleArrays()
var size = array.Length;
if (size == 0)
return array;

var maxElement = GetMaxVal(array, size);
var occurrences = new int[maxElement + 1];

for (int i = 0; i < size; i++)
{
yield return new object[] { CreateRandomArray(20000, 1, 2), "Best Case" };
yield return new object[] { CreateRandomArray(20000, 10000, 30000), "Average Case" };
yield return new object[] { CreateImbalancedArray(CreateRandomArray(19999, 10000, 30000)), "Worst Case" };
occurrences[array[i]]++;
}

[Benchmark]
[ArgumentsSource(nameof(SampleArrays))]
public int[] CountingSort(int[] array, string arrayName)
for (int i = 0, j = 0; i <= maxElement; i++)
{
var size = array.Length;
var maxElement = GetMaxVal(array, size);
var occurrences = new int[maxElement + 1];

for (int i = 0; i < maxElement + 1; i++)
while (occurrences[i] > 0)
{
occurrences[i] = 0;
array[j] = i;
j++;
occurrences[i]--;
}
}

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

for (int i = 0, j = 0; i <= maxElement; i++)
{
while (occurrences[i] > 0)
{
array[j] = i;
j++;
occurrences[i]--;
}
}
public static int GetMaxVal(int[] array, int size)
{
var maxVal = array[0];

return array;
}
for (int i = 1; i < size; i++)
if (array[i] > maxVal)
maxVal = array[i];

public static int GetMaxVal(int[] array, int size)
{
var maxVal = array[0];
return maxVal;
}

for (int i = 1; i < size; i++)
if (array[i] > maxVal)
maxVal = array[i];
public static T[] StableCountingSort<T>(T[] items, Func<T, int> getKey)
{
if (items.Length == 0)
return [];

return maxVal;
}
var min = items.Min(getKey);
var max = items.Max(getKey);
var range = (long)max - min + 1;
if (range > Array.MaxLength)
throw new ArgumentOutOfRangeException(nameof(items), "The key range is too wide for counting sort.");


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

for (int i = 0; i < size; i++)
array[i] = rand.Next(lower, upper);
foreach (var item in items)
counts[getKey(item) - min]++;

return array;
}
for (int i = 1; i < counts.Length; i++)
counts[i] += counts[i - 1];

var sorted = new T[items.Length];

public static int[] CreateImbalancedArray(int[] array)
for (int i = items.Length - 1; i >= 0; i--)
{
List<int> numbers = new List<int>();
var index = getKey(items[i]) - min;
counts[index]--;
sorted[counts[index]] = items[i];
}

numbers.AddRange(array);
numbers.Add(Int32.MaxValue/2);
return sorted;
}

return numbers.ToArray();
}
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] = rand.Next(lower, upper);

return array;
}

public static int[] CreateImbalancedArray(int[] array) => [.. array, 2_000_000];
}
2 changes: 1 addition & 1 deletion csharp-algorithms/CountingSort/CountingSort/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using BenchmarkDotNet.Running;
using CountingSort;

var summary = BenchmarkRunner.Run<CountingSortMethods>();
BenchmarkRunner.Run<CountingSortBenchmarks>(args: args);
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
Loading
Loading