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
112 changes: 50 additions & 62 deletions csharp-algorithms/BubbleSort/BubbleSort/Bubble.cs
Original file line number Diff line number Diff line change
@@ -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<int[]> 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;
}
}
43 changes: 43 additions & 0 deletions csharp-algorithms/BubbleSort/BubbleSort/BubbleBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
4 changes: 2 additions & 2 deletions csharp-algorithms/BubbleSort/BubbleSort/BubbleSort.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>
2 changes: 1 addition & 1 deletion csharp-algorithms/BubbleSort/BubbleSort/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using BenchmarkDotNet.Running;
using BubbleSort;

var summary = BenchmarkRunner.Run<Bubble>();
BenchmarkRunner.Run<BubbleBenchmarks>(args: args);
15 changes: 10 additions & 5 deletions csharp-algorithms/BubbleSort/BubbleSortTest/BubbleSortTest.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
<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="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<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>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>

<ItemGroup>
Expand Down
119 changes: 65 additions & 54 deletions csharp-algorithms/BubbleSort/BubbleSortTest/BubbleSortTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Loading