-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from kamendov-maxim/BubbleSort
Bubble sort
- Loading branch information
Showing
5 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |