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
2 changes: 1 addition & 1 deletion csharp-algorithms/ShellSort/ShellSort/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using BenchmarkDotNet.Running;
using ShellSort;

var summary = BenchmarkRunner.Run<ShellSortMethods>();
BenchmarkRunner.Run<ShellSortBenchmarks>(args: args);
4 changes: 2 additions & 2 deletions csharp-algorithms/ShellSort/ShellSort/ShellSort.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>
36 changes: 36 additions & 0 deletions csharp-algorithms/ShellSort/ShellSort/ShellSortBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using BenchmarkDotNet.Attributes;

namespace ShellSort;

public class ShellSortBenchmarks
{
private readonly ShellSortMethods _sortMethods = new();
private int[] _source = [];
private int[] _work = [];

[Params(4_096, 16_384, 65_536)]
public int Size { get; set; }

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

[GlobalSetup]
public void Setup()
{
_source = Input switch
{
"Sorted" => ShellSortMethods.CreateSortedArray(Size),
"Random" => ShellSortMethods.CreateRandomArray(Size, 1, 2_000_000),
_ => ShellSortMethods.CreateImbalancedArray(Size, 1, 2_000_000)
};
_work = new int[Size];
}

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

return _sortMethods.ShellSort(_work, Size);
}
}
102 changes: 45 additions & 57 deletions csharp-algorithms/ShellSort/ShellSort/ShellSortMethods.cs
Original file line number Diff line number Diff line change
@@ -1,79 +1,67 @@
using BenchmarkDotNet.Attributes;
namespace ShellSort;

namespace ShellSort
public class ShellSortMethods
{
public class ShellSortMethods
public int[] ShellSort(int[] array, int size)
{
public IEnumerable<object[]> SampleArrays()
for (int interval = size / 2; interval > 0; interval /= 2)
{
yield return new object[] { CreateSortedArray(2000000), 2000000, "Best Case" };
yield return new object[] { CreateRandomArray(2000000, 1, 2000000), 2000000, "Average Case" };
yield return new object[] { CreateImbalancedArray(2000000, 1, 2000000), 2000000, "Worst Case" };
}

[Benchmark]
[ArgumentsSource(nameof(SampleArrays))]
public int[] ShellSort(int[] array, int size, string arrayName)
{
for (int interval = size / 2; interval > 0; interval /= 2)
for (int i = interval; i < size; i++)
{
for (int i = interval; i < size; i++)
{
var currentKey = array[i];
var k = i;
var currentKey = array[i];
var k = i;

while (k >= interval && array[k - interval] > currentKey)
{
array[k] = array[k - interval];
k -= interval;
}

array[k] = currentKey;
while (k >= interval && array[k - interval] > currentKey)
{
array[k] = array[k - interval];
k -= interval;
}
}

return array;
array[k] = currentKey;
}
}

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

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

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

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

for (int i = 0; i < size; i++)
array[i] = i;
public static int[] CreateSortedArray(int size)
{
var array = new int[size];

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

public static int[] CreateImbalancedArray(int size, int lower, int upper)
{
var array = new int[size];
var rand = new Random();
return array;
}

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

for (int i = 0; i < size; i++)
{
if ((i % 2) == 0)
{
if ((i % 2) == 0)
{
array[i] = rand.Next(upper/2, upper);
}
else
{
array[i] = rand.Next(lower, 1000);
}
array[i] = rand.Next(upper/2, upper);
}

return array;
else
{
array[i] = rand.Next(lower, 1000);
}
}

return array;
}
}
10 changes: 5 additions & 5 deletions csharp-algorithms/ShellSort/ShellSortTests/ShellSortTests.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
109 changes: 70 additions & 39 deletions csharp-algorithms/ShellSort/ShellSortTests/ShellSortUnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,86 @@
using ShellSort;
namespace ShellSortTests

namespace ShellSortTests;

[TestClass]
public class ShellSortUnitTests
{
[TestClass]
public class ShellSortUnitTests
[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 ShellSortMethods();
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 ShellSortMethods();

var sortedArray = sortFunction.ShellSort(array, array.Length, string.Empty);
var sortedArray = sortFunction.ShellSort(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 ShellSortMethods();
var array = ShellSortMethods.CreateRandomArray(200, 0, 200);
[TestMethod]
public void GivenUnsortedArray_WhenArrayIsRandomized_ThenCheckInstanceType()
{
var sortFunction = new ShellSortMethods();
var array = ShellSortMethods.CreateRandomArray(200, 0, 200);

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

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

[TestMethod]
public void GivenUnsortedArray_WhenArrayIsSorted_ThenResultingArrayIsNotNull()
{
var sortFunction = new ShellSortMethods(); ;
var array = ShellSortMethods.CreateRandomArray(200, 0, 200);
[TestMethod]
public void GivenUnsortedArray_WhenArrayIsSorted_ThenResultingArrayIsNotNull()
{
var sortFunction = new ShellSortMethods();
var array = ShellSortMethods.CreateRandomArray(200, 0, 200);

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

Assert.IsNotNull(sortedArray);
}
Assert.IsNotNull(sortedArray);
}

[TestMethod]
public void GivenArray_WhenArrayHasSingleElement_ThenReturnSortedArray()
[TestMethod]
public void GivenArray_WhenArrayHasSingleElement_ThenReturnSortedArray()
{
var array = new int[] { 73 };
var expected = new int[] { 73 };
var sortFunction = new ShellSortMethods();

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

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

[TestMethod]
public void GivenEmptyArray_WhenSorted_ThenReturnEmptyArray()
{
var sortFunction = new ShellSortMethods();

var sortedArray = sortFunction.ShellSort([], 0);

Assert.IsEmpty(sortedArray);
}

[TestMethod]
[DataRow("Random")]
[DataRow("Sorted")]
[DataRow("Imbalanced")]
public void GivenGeneratedArray_WhenSorted_ThenMatchesArraySort(string input)
{
var array = input switch
{
var array = new int[] { 73 };
var expected = new int[] { 73 };
var sortFunction = new ShellSortMethods();
"Random" => ShellSortMethods.CreateRandomArray(1_000, 0, 50),
"Sorted" => ShellSortMethods.CreateSortedArray(1_000),
_ => ShellSortMethods.CreateImbalancedArray(1_024, 1, 2_000_000)
};
var expected = (int[])array.Clone();
Array.Sort(expected);
var sortFunction = new ShellSortMethods();

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

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