-
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.
- Loading branch information
1 parent
56af417
commit 1351933
Showing
8 changed files
with
182 additions
and
4 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
Behavioral/DesignPatterns.Strategy/DesignPatterns.Strategy.csproj
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,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> |
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,6 @@ | ||
namespace DesignPatterns.Strategy; | ||
|
||
public interface ISortingStrategy | ||
{ | ||
void Sort<T>(List<T> list) where T : IComparable<T>; | ||
} |
49 changes: 49 additions & 0 deletions
49
Behavioral/DesignPatterns.Strategy/MergeSortingStrategy.cs
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,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; | ||
} | ||
} |
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,4 @@ | ||
using BenchmarkDotNet.Running; | ||
using DesignPatterns.Strategy; | ||
|
||
var summary = BenchmarkRunner.Run<SortingBenchmark>(); |
39 changes: 39 additions & 0 deletions
39
Behavioral/DesignPatterns.Strategy/QuickSortingStrategy.cs
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,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; | ||
} | ||
} |
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,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); | ||
} | ||
} |
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
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