Skip to content

Commit

Permalink
Merge pull request #14 from kamendov-maxim/BubbleSort
Browse files Browse the repository at this point in the history
Bubble sort
  • Loading branch information
kamendov-maxim authored Oct 10, 2024
2 parents 7ad6433 + 1907510 commit 33c5935
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
9 changes: 9 additions & 0 deletions BubbleSort/BubbleSort.Src/BubbleSort.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
35 changes: 35 additions & 0 deletions BubbleSort/BubbleSort.Src/Sort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace finalTest1;

/// <summary>
/// Class containing generic bubble sort implementation
/// </summary>
public class Sorting
{
/// <summary>
/// Generic bubble sort implementation
/// </summary>
/// <typeparam name="T">Type implementing IComparable interface</typeparam>
/// <param name="list">list to sort</param>
/// <param name="comparer">IComparer which will be used to sort elements in the list</param>
public static void BubbleSort<T>(IList<T> list, IComparer<T> comparer)
{
ArgumentNullException.ThrowIfNull(list, "List is null");
ArgumentNullException.ThrowIfNull(comparer, "Comparer is null");
for (int i = 0; i < list.Count - 1; ++i)
{
bool swapped = false;
for (int j = 0; j < list.Count - 1; ++j)
{
if (comparer.Compare(list[j], list[j + 1]) > 0)
{
(list[j], list[j + 1]) = (list[j + 1], list[j]);
swapped = true;
}
}
if (!swapped)
{
break;
}
}
}
}
41 changes: 41 additions & 0 deletions BubbleSort/BubbleSort.Tests/BubbleSort.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace BubbleSort.Tests;
using finalTest1;

public class Tests
{
private readonly int[] intArray = [6, 3, 89, 54, 2, 5, 2, 0, 1, 4, 7, 34, 76, 2, 6];
private readonly string[] stringArray = ["ddd", "aa", "c", "bbbb"];
private readonly List<int>[] arrayOfLists = [[2, 2], [1], [4, 4, 4, 4], [3, 3, 3]];

[Test]
public void TestBubbleSortWithStringsAndComparerByLength()
{
string[] expectedResult = ["c", "aa", "ddd", "bbbb"];
Sorting.BubbleSort(stringArray, Comparer<string>.Create((string a, string b) => a.Length - b.Length));
Assert.That(stringArray, Is.EqualTo(expectedResult));
}

[Test]
public void TestBubbleSortOnArrayOfLists()
{
List<int>[] expectedResult = [[1], [2, 2], [3, 3, 3], [4, 4, 4, 4]];
Sorting.BubbleSort<List<int>>(arrayOfLists, Comparer<List<int>>.Create((List<int> a, List<int> b) => a.Count - b.Count));
Assert.That(arrayOfLists, Is.EqualTo(expectedResult));
}

[Test]
public void TestBubbleSortWithIntsAndDefaultComparer()
{
int[] expectedResult = [0, 1, 2, 2, 2, 3, 4, 5, 6, 6, 7, 34, 54, 76, 89];
Sorting.BubbleSort(intArray, Comparer<int>.Default);
Assert.That(intArray, Is.EqualTo(expectedResult));
}

[Test]
public void TestBubbleSortWIthStringsAndDefaultComparer()
{
string[] expectedResult = ["aa", "bbbb", "c", "ddd"];
Sorting.BubbleSort(stringArray, Comparer<string>.Default);
Assert.That(stringArray, Is.EqualTo(expectedResult));
}
}
28 changes: 28 additions & 0 deletions BubbleSort/BubbleSort.Tests/BubbleSort.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BubbleSort.Src\BubbleSort.csproj" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions BubbleSort/BubbleSort.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BubbleSort", "BubbleSort.Src\BubbleSort.csproj", "{6BADF4B9-D831-46C1-8179-E549B396E33D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BubbleSort.Tests", "BubbleSort.Tests\BubbleSort.Tests.csproj", "{8ACBC5F2-5CE4-4A19-85A3-CF209E6832A2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6BADF4B9-D831-46C1-8179-E549B396E33D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6BADF4B9-D831-46C1-8179-E549B396E33D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6BADF4B9-D831-46C1-8179-E549B396E33D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6BADF4B9-D831-46C1-8179-E549B396E33D}.Release|Any CPU.Build.0 = Release|Any CPU
{8ACBC5F2-5CE4-4A19-85A3-CF209E6832A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8ACBC5F2-5CE4-4A19-85A3-CF209E6832A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8ACBC5F2-5CE4-4A19-85A3-CF209E6832A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8ACBC5F2-5CE4-4A19-85A3-CF209E6832A2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

0 comments on commit 33c5935

Please sign in to comment.