diff --git a/csharp-algorithms/ShellSort/ShellSort/Program.cs b/csharp-algorithms/ShellSort/ShellSort/Program.cs index a8aab3314a..1d28d667cf 100644 --- a/csharp-algorithms/ShellSort/ShellSort/Program.cs +++ b/csharp-algorithms/ShellSort/ShellSort/Program.cs @@ -1,4 +1,4 @@ using BenchmarkDotNet.Running; using ShellSort; -var summary = BenchmarkRunner.Run(); \ No newline at end of file +BenchmarkRunner.Run(args: args); diff --git a/csharp-algorithms/ShellSort/ShellSort/ShellSort.csproj b/csharp-algorithms/ShellSort/ShellSort/ShellSort.csproj index 79e7148fd0..62be4f867f 100644 --- a/csharp-algorithms/ShellSort/ShellSort/ShellSort.csproj +++ b/csharp-algorithms/ShellSort/ShellSort/ShellSort.csproj @@ -2,13 +2,13 @@ Exe - net6.0 + net10.0 enable enable - + diff --git a/csharp-algorithms/ShellSort/ShellSort/ShellSortBenchmarks.cs b/csharp-algorithms/ShellSort/ShellSort/ShellSortBenchmarks.cs new file mode 100644 index 0000000000..71c4aad658 --- /dev/null +++ b/csharp-algorithms/ShellSort/ShellSort/ShellSortBenchmarks.cs @@ -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); + } +} diff --git a/csharp-algorithms/ShellSort/ShellSort/ShellSortMethods.cs b/csharp-algorithms/ShellSort/ShellSort/ShellSortMethods.cs index af00ff27e7..b980afbcfd 100644 --- a/csharp-algorithms/ShellSort/ShellSort/ShellSortMethods.cs +++ b/csharp-algorithms/ShellSort/ShellSort/ShellSortMethods.cs @@ -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 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; } } diff --git a/csharp-algorithms/ShellSort/ShellSortTests/ShellSortTests.csproj b/csharp-algorithms/ShellSort/ShellSortTests/ShellSortTests.csproj index fa238fbb1b..5a5971414f 100644 --- a/csharp-algorithms/ShellSort/ShellSortTests/ShellSortTests.csproj +++ b/csharp-algorithms/ShellSort/ShellSortTests/ShellSortTests.csproj @@ -1,7 +1,7 @@ - net6.0 + net10.0 enable enable @@ -9,10 +9,10 @@ - - - - + + + + diff --git a/csharp-algorithms/ShellSort/ShellSortTests/ShellSortUnitTests.cs b/csharp-algorithms/ShellSort/ShellSortTests/ShellSortUnitTests.cs index 47f4ad652d..a4d35f4399 100644 --- a/csharp-algorithms/ShellSort/ShellSortTests/ShellSortUnitTests.cs +++ b/csharp-algorithms/ShellSort/ShellSortTests/ShellSortUnitTests.cs @@ -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); } -} \ No newline at end of file +}