diff --git a/csharp-algorithms/BubbleSort/BubbleSort/Bubble.cs b/csharp-algorithms/BubbleSort/BubbleSort/Bubble.cs index 0daea131f3..6d44dd0e18 100644 --- a/csharp-algorithms/BubbleSort/BubbleSort/Bubble.cs +++ b/csharp-algorithms/BubbleSort/BubbleSort/Bubble.cs @@ -1,80 +1,68 @@ -using BenchmarkDotNet.Attributes; +namespace BubbleSort; -namespace BubbleSort +public class Bubble { - public class Bubble + public int[] NumArray { get; set; } = []; + + public int[] SortArray() { - [ParamsSource(nameof(ArraySizes))] - public int[]? NumArray { get; set; } - int[] smallArray = GenerateRandomNumber(200); - int[] mediumArray = GenerateRandomNumber(2000); - int[] largeArray = GenerateRandomNumber(200000); - int[] sortedSmallArray = GenerateSortedNumber(200); - int[] sortedMediumArray = GenerateSortedNumber(2000); - int[] sortedLargeArray = GenerateSortedNumber(200000); - public IEnumerable ArraySizes => new[] { sortedSmallArray, sortedMediumArray, sortedLargeArray }; + var n = NumArray.Length; - [Benchmark] - public int[] SortArray() - { - var n = NumArray.Length; + for (int i = 0; i < n - 1; i++) + for (int j = 0; j < n - i - 1; j++) + if (NumArray[j] > NumArray[j + 1]) + { + var tempVar = NumArray[j]; + NumArray[j] = NumArray[j + 1]; + NumArray[j + 1] = tempVar; + } - for (int i = 0; i < n - 1; i++) - for (int j = 0; j < n - i - 1; j++) - if (NumArray[j] > NumArray[j + 1]) - { - var tempVar = NumArray[j]; - NumArray[j] = NumArray[j + 1]; - NumArray[j + 1] = tempVar; - } + return NumArray; + } - return NumArray; - } + public int[] SortOptimizedArray() + { + var n = NumArray.Length; + bool swapRequired; - [Benchmark] - public int[] SortOptimizedArray() + for (int i = 0; i < n - 1; i++) { - var n = NumArray.Length; - bool swapRequired; + swapRequired = false; - for (int i = 0; i < n - 1; i++) - { - swapRequired = false; - for (int j = 0; j < n - i - 1; j++) - if (NumArray[j] > NumArray[j + 1]) - { - var tempVar = NumArray[j]; - NumArray[j] = NumArray[j + 1]; - NumArray[j + 1] = tempVar; - swapRequired = true; - } - if (swapRequired == false) - break; - } - - return NumArray; + for (int j = 0; j < n - i - 1; j++) + if (NumArray[j] > NumArray[j + 1]) + { + var tempVar = NumArray[j]; + NumArray[j] = NumArray[j + 1]; + NumArray[j + 1] = tempVar; + swapRequired = true; + } + if (swapRequired == false) + break; } - public static int[] GenerateRandomNumber(int size) - { - var array = new int[size]; - var rand = new Random(); - var maxNum = 10000; + return NumArray; + } - for (int i = 0; i < size; i++) - array[i] = rand.Next(maxNum + 1); + public static int[] GenerateRandomNumber(int size) + { + var array = new int[size]; + var rand = new Random(); + var maxNum = 10000; - return array; - } + for (int i = 0; i < size; i++) + array[i] = rand.Next(maxNum + 1); - public static int[] GenerateSortedNumber(int size) - { - var array = new int[size]; + return array; + } - for (int i = 0; i < size; i++) - array[i] = i; + public static int[] GenerateSortedNumber(int size) + { + var array = new int[size]; - return array; - } + for (int i = 0; i < size; i++) + array[i] = i; + + return array; } } diff --git a/csharp-algorithms/BubbleSort/BubbleSort/BubbleBenchmarks.cs b/csharp-algorithms/BubbleSort/BubbleSort/BubbleBenchmarks.cs new file mode 100644 index 0000000000..1833642d65 --- /dev/null +++ b/csharp-algorithms/BubbleSort/BubbleSort/BubbleBenchmarks.cs @@ -0,0 +1,43 @@ +using BenchmarkDotNet.Attributes; + +namespace BubbleSort; + +public class BubbleBenchmarks +{ + private readonly Bubble _bubble = new(); + private int[] _source = []; + private int[] _work = []; + + [Params(200, 2_000, 20_000)] + public int Size { get; set; } + + [Params("Random", "Sorted")] + public string Input { get; set; } = "Random"; + + [GlobalSetup] + public void Setup() + { + _source = Input == "Random" + ? Bubble.GenerateRandomNumber(Size) + : Bubble.GenerateSortedNumber(Size); + _work = new int[Size]; + } + + [Benchmark] + public int[] SortArray() + { + _source.CopyTo(_work, 0); + _bubble.NumArray = _work; + + return _bubble.SortArray(); + } + + [Benchmark] + public int[] SortOptimizedArray() + { + _source.CopyTo(_work, 0); + _bubble.NumArray = _work; + + return _bubble.SortOptimizedArray(); + } +} diff --git a/csharp-algorithms/BubbleSort/BubbleSort/BubbleSort.csproj b/csharp-algorithms/BubbleSort/BubbleSort/BubbleSort.csproj index 79e7148fd0..62be4f867f 100644 --- a/csharp-algorithms/BubbleSort/BubbleSort/BubbleSort.csproj +++ b/csharp-algorithms/BubbleSort/BubbleSort/BubbleSort.csproj @@ -2,13 +2,13 @@ Exe - net6.0 + net10.0 enable enable - + diff --git a/csharp-algorithms/BubbleSort/BubbleSort/Program.cs b/csharp-algorithms/BubbleSort/BubbleSort/Program.cs index ca7f64e662..a97b34b761 100644 --- a/csharp-algorithms/BubbleSort/BubbleSort/Program.cs +++ b/csharp-algorithms/BubbleSort/BubbleSort/Program.cs @@ -1,4 +1,4 @@ using BenchmarkDotNet.Running; using BubbleSort; -var summary = BenchmarkRunner.Run(); \ No newline at end of file +BenchmarkRunner.Run(args: args); diff --git a/csharp-algorithms/BubbleSort/BubbleSortTest/BubbleSortTest.csproj b/csharp-algorithms/BubbleSort/BubbleSortTest/BubbleSortTest.csproj index be3c46c73a..a5d08fd0fa 100644 --- a/csharp-algorithms/BubbleSort/BubbleSortTest/BubbleSortTest.csproj +++ b/csharp-algorithms/BubbleSort/BubbleSortTest/BubbleSortTest.csproj @@ -1,17 +1,22 @@ - net6.0 + net10.0 + enable enable false - - - - + + + + + + + + diff --git a/csharp-algorithms/BubbleSort/BubbleSortTest/BubbleSortTests.cs b/csharp-algorithms/BubbleSort/BubbleSortTest/BubbleSortTests.cs index 451378c346..3afaa316ee 100644 --- a/csharp-algorithms/BubbleSort/BubbleSortTest/BubbleSortTests.cs +++ b/csharp-algorithms/BubbleSort/BubbleSortTest/BubbleSortTests.cs @@ -1,59 +1,70 @@ using BubbleSort; -using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace BubbleSortTest +namespace BubbleSortTest; + +[TestClass] +public class BubbleSortTests { - [TestClass] - public class BubbleSortTests + [TestMethod] + public void GivenUnsortedArray_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 Bubble(); + sortFunction.NumArray = array; + + var sortedArray = sortFunction.SortArray(); + + Assert.IsNotNull(sortedArray); + CollectionAssert.AreEqual(expected, sortedArray); + } + + [TestMethod] + public void GivenUnsortedArray_WhenSortedWithOptimizedBubbleSort_ThenGetSortedArray() { - [TestMethod] - public void GivenUnsortedArray_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 Bubble(); - sortFunction.NumArray = array; - - var sortedArray = sortFunction.SortArray(); - - Assert.IsNotNull(sortedArray); - CollectionAssert.AreEqual(sortedArray, expected); - } - - [TestMethod] - public void GivenUnsortedArray_WhenSortedWithOptimizedBubbleSort_ThenGetSortedArray() - { - 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 Bubble(); - - sortFunction.NumArray = array; - var sortedArray = sortFunction.SortOptimizedArray(); - - Assert.IsNotNull(sortedArray); - CollectionAssert.AreEqual(sortedArray, expected); - } - - [TestMethod] - public void GivenUnsortedArray_CheckInstanceType() - { - var sortFunction = new Bubble(); - sortFunction.NumArray = Bubble.GenerateRandomNumber(200); - - var sortedArray = sortFunction.SortArray(); - - Assert.IsInstanceOfType(sortedArray, typeof(int[])); - } - - [TestMethod] - public void GivenUnsortedArray_CheckSortedNotNull() - { - var sortFunction = new Bubble(); - sortFunction.NumArray = Bubble.GenerateRandomNumber(200); - - var sortedArray = sortFunction.SortArray(); - - Assert.IsNotNull(sortedArray); - } + 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 Bubble(); + + sortFunction.NumArray = array; + var sortedArray = sortFunction.SortOptimizedArray(); + + Assert.IsNotNull(sortedArray); + CollectionAssert.AreEqual(expected, sortedArray); + } + + [TestMethod] + public void GivenUnsortedArray_CheckInstanceType() + { + var sortFunction = new Bubble(); + sortFunction.NumArray = Bubble.GenerateRandomNumber(200); + + var sortedArray = sortFunction.SortArray(); + + Assert.IsInstanceOfType(sortedArray, typeof(int[])); + } + + [TestMethod] + public void GivenUnsortedArray_CheckSortedNotNull() + { + var sortFunction = new Bubble(); + sortFunction.NumArray = Bubble.GenerateRandomNumber(200); + + var sortedArray = sortFunction.SortArray(); + + Assert.IsNotNull(sortedArray); + } + + [TestMethod] + public void GivenRandomArray_WhenSortedWithBothVersions_ThenResultsMatch() + { + var source = Bubble.GenerateRandomNumber(500); + var expected = source.Order().ToArray(); + + var plain = new Bubble { NumArray = source.ToArray() }.SortArray(); + var optimized = new Bubble { NumArray = source.ToArray() }.SortOptimizedArray(); + + CollectionAssert.AreEqual(expected, plain); + CollectionAssert.AreEqual(expected, optimized); } -} \ No newline at end of file +}