Skip to content

Commit

Permalink
Add Strategy.
Browse files Browse the repository at this point in the history
  • Loading branch information
eminencegrs committed Nov 1, 2024
1 parent 56af417 commit 1351933
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 4 deletions.
14 changes: 14 additions & 0 deletions Behavioral/DesignPatterns.Strategy/DesignPatterns.Strategy.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions Behavioral/DesignPatterns.Strategy/ISortingStrategy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace DesignPatterns.Strategy;

public interface ISortingStrategy
{
void Sort<T>(List<T> list) where T : IComparable<T>;
}
49 changes: 49 additions & 0 deletions Behavioral/DesignPatterns.Strategy/MergeSortingStrategy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace DesignPatterns.Strategy;

public class MergeSortingStrategy : ISortingStrategy
{
public void Sort<T>(List<T> list) where T : IComparable<T>
{
list = this.MergeSort(list);
}

private List<T> MergeSort<T>(List<T> list) where T : IComparable<T>
{
if (list.Count <= 1)
{
return list;
}

var middle = list.Count / 2;
var left = this.MergeSort(list.GetRange(0, middle));
var right = this.MergeSort(list.GetRange(middle, list.Count - middle));

return this.Merge(left, right);
}

private List<T> Merge<T>(List<T> left, List<T> right) where T : IComparable<T>
{
var result = new List<T>();
var i = 0;
var j = 0;

while (i < left.Count && j < right.Count)
{
if (left[i].CompareTo(right[j]) < 0)
{
result.Add(left[i]);
i++;
}
else
{
result.Add(right[j]);
j++;
}
}

result.AddRange(left.GetRange(i, left.Count - i));
result.AddRange(right.GetRange(j, right.Count - j));

return result;
}
}
4 changes: 4 additions & 0 deletions Behavioral/DesignPatterns.Strategy/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using BenchmarkDotNet.Running;
using DesignPatterns.Strategy;

var summary = BenchmarkRunner.Run<SortingBenchmark>();
39 changes: 39 additions & 0 deletions Behavioral/DesignPatterns.Strategy/QuickSortingStrategy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace DesignPatterns.Strategy;

public class QuickSortingStrategy : ISortingStrategy
{
public void Sort<T>(List<T> list) where T : IComparable<T>
{
this.QuickSort(list, 0, list.Count - 1);
}

private void QuickSort<T>(List<T> list, int low, int high) where T : IComparable<T>
{
if (low >= high)
{
return;
}

var pivotIndex = this.Partition(list, low, high);
this.QuickSort(list, low, pivotIndex - 1);
this.QuickSort(list, pivotIndex + 1, high);
}

private int Partition<T>(List<T> list, int low, int high) where T : IComparable<T>
{
T pivot = list[high];
var i = low - 1;

for (var j = low; j < high; j++)
{
if (list[j].CompareTo(pivot) < 0)
{
i++;
(list[i], list[j]) = (list[j], list[i]);
}
}

(list[i + 1], list[high]) = (list[high], list[i + 1]);
return i + 1;
}
}
63 changes: 63 additions & 0 deletions Behavioral/DesignPatterns.Strategy/SortingBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using BenchmarkDotNet.Attributes;

namespace DesignPatterns.Strategy;

public class SortingBenchmark
{
private readonly ISortingStrategy quickSortingStrategy = new QuickSortingStrategy();
private readonly ISortingStrategy mergeSortingStrategy = new MergeSortingStrategy();

private List<int>? unsortedList;
private List<int>? sortedList;
private List<int>? reverseSortedList;

[GlobalSetup]
public void Setup()
{
this.unsortedList = [34, 7, 23, 32, 5, 62, 12, 19, 4, 47];
this.sortedList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
this.reverseSortedList = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
}

[Benchmark]
public void QuickSortUnsortedBenchmark()
{
var list = new List<int>(this.unsortedList!);
this.quickSortingStrategy.Sort(list);
}

[Benchmark]
public void MergeSortUnsortedBenchmark()
{
var list = new List<int>(this.unsortedList!);
this.mergeSortingStrategy.Sort(list);
}

[Benchmark]
public void QuickSortSortedBenchmark()
{
var list = new List<int>(this.sortedList!);
this.quickSortingStrategy.Sort(list);
}

[Benchmark]
public void MergeSortSortedBenchmark()
{
var list = new List<int>(this.sortedList!);
this.mergeSortingStrategy.Sort(list);
}

[Benchmark]
public void QuickSortReverseSortedBenchmark()
{
var list = new List<int>(this.reverseSortedList!);
this.quickSortingStrategy.Sort(list);
}

[Benchmark]
public void MergeSortReverseSortedBenchmark()
{
var list = new List<int>(this.reverseSortedList!);
this.mergeSortingStrategy.Sort(list);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Shouldly" Version="4.2.1" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions DesignPatterns.sln
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.Builder", "C
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.Builder.UnitTests", "Creational\DesignPatterns.Builder.UnitTests\DesignPatterns.Builder.UnitTests.csproj", "{774BAD81-C8DA-4491-B53C-F023A8235AA7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.Strategy", "Behavioral\DesignPatterns.Strategy\DesignPatterns.Strategy.csproj", "{847A94E0-6BC0-4604-B182-0A445CC550AD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -84,6 +86,10 @@ Global
{774BAD81-C8DA-4491-B53C-F023A8235AA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{774BAD81-C8DA-4491-B53C-F023A8235AA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{774BAD81-C8DA-4491-B53C-F023A8235AA7}.Release|Any CPU.Build.0 = Release|Any CPU
{847A94E0-6BC0-4604-B182-0A445CC550AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{847A94E0-6BC0-4604-B182-0A445CC550AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{847A94E0-6BC0-4604-B182-0A445CC550AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{847A94E0-6BC0-4604-B182-0A445CC550AD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{DCE06EB6-BAB5-4573-AF67-128DBCCDB90C} = {7FF0E83F-EBEB-4103-BCD1-2F100E82FCD1}
Expand All @@ -98,5 +104,6 @@ Global
{3942358E-6D3A-441B-ABD7-7FCB0DF988BD} = {5961ADCC-5FA6-4076-A9F4-C1D3207ABE1A}
{727C96E6-5B0E-43BE-BB4E-2D06F40E51BF} = {AE005ED4-5F0B-4AF9-81AE-AAE3AD6F8901}
{774BAD81-C8DA-4491-B53C-F023A8235AA7} = {AE005ED4-5F0B-4AF9-81AE-AAE3AD6F8901}
{847A94E0-6BC0-4604-B182-0A445CC550AD} = {7FF0E83F-EBEB-4103-BCD1-2F100E82FCD1}
EndGlobalSection
EndGlobal

0 comments on commit 1351933

Please sign in to comment.