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,45 @@
using BenchmarkDotNet.Attributes;

namespace InsertionSort;

public class InsertionSortBenchmarks
{
private readonly InsertionSortMethods _sortFunction = new();
private int[] _source = [];
private int[] _work = [];

[Params(100, 1_000, 10_000)]
public int Size { get; set; }

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

[GlobalSetup]
public void Setup()
{
_source = Input switch
{
"Sorted" => InsertionSortMethods.CreateSortedArray(Size),
"Reversed" => InsertionSortMethods.CreateReversedArray(
InsertionSortMethods.CreateSortedArray(Size)),
_ => InsertionSortMethods.CreateRandomArray(Size)
};
_work = new int[Size];
}

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

return _sortFunction.SortArray(_work, _work.Length);
}

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

return _sortFunction.SortArrayRecursive(_work, _work.Length);
}
}
124 changes: 50 additions & 74 deletions csharp-algorithms/InsertionSort/InsertionSort/InsertionSortMethods.cs
Original file line number Diff line number Diff line change
@@ -1,100 +1,76 @@
using BenchmarkDotNet.Attributes;
namespace InsertionSort;

namespace InsertionSort
public class InsertionSortMethods
{
public class InsertionSortMethods
public int[] SortArray(int[] array, int length)
{
public IEnumerable<object[]> SampleArrays()
for (int i = 1; i < length; i++)
{
yield return new object[] { CreateRandomArray(200), 200, "Small Unsorted" };
yield return new object[] { CreateRandomArray(2000), 2000, "Medium Unsorted" };
yield return new object[] { CreateRandomArray(20000), 20000, "Large Unsorted" };
yield return new object[] { CreateSortedArray(200), 200, "Small Sorted" };
yield return new object[] { CreateSortedArray(2000), 2000, "Medium Sorted" };
yield return new object[] { CreateSortedArray(20000), 20000, "Large Sorted" };
yield return new object[] { CreateReversedArray(CreateSortedArray(200)), 200, "Small Reversed" };
yield return new object[] { CreateReversedArray(CreateSortedArray(2000)), 2000, "Medium Reversed" };
yield return new object[] { CreateReversedArray(CreateSortedArray(20000)), 20000, "Large Reversed" };
}

[Benchmark]
[ArgumentsSource(nameof(SampleArrays))]
public int[] SortArray(int[] array, int length, string arrayName)
{
if (length <= 1)
{
return array;
}
var key = array[i];
var flag = 0;

for (int i = 1; i < length; i++)
for (int j = i - 1; j >= 0 && flag != 1;)
{
var key = array[i];
var flag = 0;

for (int j = i - 1; j >= 0 && flag != 1;)
if (key < array[j])
{
if (key < array[j])
{
array[j + 1] = array[j];
j--;
array[j + 1] = key;
}
else flag = 1;
array[j + 1] = array[j];
j--;
array[j + 1] = key;
}
else flag = 1;
}
}

return array;
}

public int[] SortArrayRecursive(int[] array, int length)
{
if (length <= 1)
{
return array;
}

[Benchmark]
[ArgumentsSource(nameof(SampleArrays))]
public int[] SortArrayRecursive(int[] array, int length, string arrayName)
{
if (length <= 1)
{
return array;
}
SortArrayRecursive(array, length - 1);
var key = array[length - 1];
var k = length - 2;

SortArrayRecursive(array, length - 1, arrayName);
var key = array[length - 1];
var k = length - 2;
while (k >= 0 && array[k] > key)
{
array[k + 1] = array[k];
k = k - 1;
}

while (k >= 0 && array[k] > key)
{
array[k + 1] = array[k];
k = k - 1;
}
array[k + 1] = key;

array[k + 1] = key;
return array;
}

return array;
}
public static int[] CreateRandomArray(int size)
{
var array = new int[size];
var rand = new Random();
public static int[] CreateRandomArray(int size)
{
var array = new int[size];
var rand = new Random();

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

return array;
}
return array;
}

public static int[] CreateSortedArray(int size)
{
var array = new int[size];
public static int[] CreateSortedArray(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;
}

public static int[] CreateReversedArray(int[] array)
{
Array.Reverse(array);
public static int[] CreateReversedArray(int[] array)
{
Array.Reverse(array);

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

var summary = BenchmarkRunner.Run<InsertionSortMethods>();
BenchmarkRunner.Run<InsertionSortBenchmarks>(args: args);
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
<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.9" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.9" />
<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">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

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

<ItemGroup>
<ProjectReference Include="..\InsertionSort\InsertionSort.csproj" />
</ItemGroup>
Expand Down
Loading
Loading