Skip to content
Open
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
2 changes: 1 addition & 1 deletion csharp-algorithms/QuickSort/QuickSort/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using BenchmarkDotNet.Running;
using QuickSort;

var summary = BenchmarkRunner.Run<QuickSortMethods>();
BenchmarkRunner.Run<QuickSortBenchmarks>(args: args);
4 changes: 2 additions & 2 deletions csharp-algorithms/QuickSort/QuickSort/QuickSort.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>
47 changes: 47 additions & 0 deletions csharp-algorithms/QuickSort/QuickSort/QuickSortBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using BenchmarkDotNet.Attributes;

namespace QuickSort;

public class QuickSortBenchmarks
{
private readonly QuickSortMethods _sorter = new();
private int[] _source = [];
private int[] _work = [];

[Params(200, 1000, 2000)]
public int Size { get; set; }

[Params("Unsorted", "Sorted")]
public string Input { get; set; } = "Unsorted";

[GlobalSetup]
public void Setup()
{
_source = Input == "Sorted"
? QuickSortMethods.CreateSortedArray(Size)
: QuickSortMethods.CreateRandomArray(Size);
_work = new int[Size];
}

[Benchmark(Baseline = true)]
public int[] FirstElementPivot()
{
_source.CopyTo(_work, 0);
return _sorter.SortArray(_work, 0, _work.Length - 1);
}

[Benchmark]
public int[] MiddleElementPivot()
{
_source.CopyTo(_work, 0);
return _sorter.SortArrayWithMiddlePivot(_work, 0, _work.Length - 1);
}

[Benchmark]
public int[] ArraySort()
{
_source.CopyTo(_work, 0);
Array.Sort(_work);
return _work;
}
}
140 changes: 81 additions & 59 deletions csharp-algorithms/QuickSort/QuickSort/QuickSortMethods.cs
Original file line number Diff line number Diff line change
@@ -1,76 +1,98 @@
using BenchmarkDotNet.Attributes;
namespace QuickSort;

namespace QuickSort
public class QuickSortMethods
{
public class QuickSortMethods
public int[] SortArray(int[] array, int leftIndex, int rightIndex)
{
public IEnumerable<object[]> SampleArrays()
{
yield return new object[] { CreateRandomArray(200), 0, 199, "Small Unsorted" };
yield return new object[] { CreateRandomArray(2000), 0, 1999, "Medium Unsorted" };
yield return new object[] { CreateRandomArray(10000), 0, 9999, "Large Unsorted" };
yield return new object[] { CreateSortedArray(200), 0, 199, "Small Sorted" };
yield return new object[] { CreateSortedArray(2000), 0, 1999, "Medium Sorted" };
yield return new object[] { CreateSortedArray(10000), 0, 9999, "Large Sorted" };
}

[Benchmark]
[ArgumentsSource(nameof(SampleArrays))]
public int[] SortArray(int[] array, int leftIndex, int rightIndex, string arrayName)
{
var i = leftIndex;
var j = rightIndex;
var pivot = array[leftIndex];
var i = leftIndex;
var j = rightIndex;
var pivot = array[leftIndex];

while (i <= j)
while (i <= j)
{
while (array[i] < pivot)
{
while (array[i] < pivot)
{
i++;
}

while (array[j] > pivot)
{
j--;
}

if (i <= j)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
i++;
}

if (leftIndex < j)
SortArray(array, leftIndex, j, arrayName);

if (i < rightIndex)
SortArray(array, i, rightIndex, arrayName);
while (array[j] > pivot)
{
j--;
}

return array;
if (i <= j)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
}

public static int[] CreateRandomArray(int size)
{
var array = new int[size];
var rand = new Random();

for (int i = 0; i < size; i++)
array[i] = rand.Next();
if (leftIndex < j)
SortArray(array, leftIndex, j);

return array;
}
public static int[] CreateSortedArray(int size)
if (i < rightIndex)
SortArray(array, i, rightIndex);

return array;
}

public int[] SortArrayWithMiddlePivot(int[] array, int leftIndex, int rightIndex)
{
var i = leftIndex;
var j = rightIndex;
var pivot = array[leftIndex + (rightIndex - leftIndex) / 2];

while (i <= j)
{
var array = new int[size];
while (array[i] < pivot)
{
i++;
}

for (int i = 0; i < size; i++)
array[i] = i;
while (array[j] > pivot)
{
j--;
}

return array;
if (i <= j)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
}

if (leftIndex < j)
SortArrayWithMiddlePivot(array, leftIndex, j);

if (i < rightIndex)
SortArrayWithMiddlePivot(array, i, rightIndex);

return array;
}

public static int[] CreateRandomArray(int size)
{
var array = new int[size];

for (int i = 0; i < size; i++)
array[i] = Random.Shared.Next();

return array;
}

public static int[] CreateSortedArray(int size)
{
var array = new int[size];

for (int i = 0; i < size; i++)
array[i] = i;

return array;
}
}
12 changes: 6 additions & 6 deletions csharp-algorithms/QuickSort/QuickSortTests/QuickSortTests.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<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="17.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.9" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.9" />
<PackageReference Include="coverlet.collector" Version="3.1.2">
<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">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
77 changes: 45 additions & 32 deletions csharp-algorithms/QuickSort/QuickSortTests/QuickSortUnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,59 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QuickSort;
using System;

namespace QuickSortTests
namespace QuickSortTests;

[TestClass]
public class QuickSortUnitTests
{
[TestClass]
public class QuickSortUnitTests
[TestMethod]
public void GivenUnsortedArray_WhenArrayIsNotEmpty_ThenReturnSortedArray()
{
[TestMethod]
public void GivenUnsortedArray_WhenArrayIsNotEmpty_ThenReturnSortedArray()
{
var array = new int[] { 73, 57, 49, 99, 133, 20, 1, 73, 57, 49, 99, 133, 20, 1 };
var expected = new int[] { 1, 1,20, 20, 49, 49, 57, 57, 73, 73, 99, 99, 133, 133 };

var sortFunction = new QuickSortMethods();
var array = new int[] { 73, 57, 49, 99, 133, 20, 1, 73, 57, 49, 99, 133, 20, 1 };
var expected = new int[] { 1, 1, 20, 20, 49, 49, 57, 57, 73, 73, 99, 99, 133, 133 };

var sortFunction = new QuickSortMethods();

var sortedArray = sortFunction.SortArray(array, 0, array.Length - 1, string.Empty);
var sortedArray = sortFunction.SortArray(array, 0, array.Length - 1);

Assert.IsNotNull(sortedArray);
CollectionAssert.AreEqual(sortedArray, expected);
}
Assert.IsNotNull(sortedArray);
CollectionAssert.AreEqual(expected, sortedArray);
}

[TestMethod]
public void GivenUnsortedArray_WhenArrayIsNotEmpty_ThenCheckInstanceType()
{
var sortFunction = new QuickSortMethods();
var array = QuickSortMethods.CreateRandomArray(200);
[TestMethod]
public void GivenArticleArray_WhenSorted_ThenAllNineElementsAreInOrder()
{
int[] array = { 52, 96, 67, 71, 42, 38, 39, 40, 14 };
var expected = new int[] { 14, 38, 39, 40, 42, 52, 67, 71, 96 };

var sortedArray = sortFunction.SortArray(array, 0, array.Length - 1, string.Empty);
var sortedArray = new QuickSortMethods().SortArray(array, 0, array.Length - 1);

Assert.IsInstanceOfType(sortedArray, typeof(int[]));
}
CollectionAssert.AreEqual(expected, sortedArray);
}

[TestMethod]
public void GivenUnsortedArray_WhenArrayIsSorted_ThenResultingArrayIsNotNull()
{
var sortFunction = new QuickSortMethods();
var array = QuickSortMethods.CreateRandomArray(5);
[TestMethod]
public void GivenRandomArray_WhenSortedWithEitherPivot_ThenMatchesArraySort()
{
var sortFunction = new QuickSortMethods();
var source = QuickSortMethods.CreateRandomArray(2000);
var expected = (int[])source.Clone();
Array.Sort(expected);

var firstPivot = sortFunction.SortArray((int[])source.Clone(), 0, source.Length - 1);
var middlePivot = sortFunction.SortArrayWithMiddlePivot((int[])source.Clone(), 0, source.Length - 1);

CollectionAssert.AreEqual(expected, firstPivot);
CollectionAssert.AreEqual(expected, middlePivot);
}

[TestMethod]
public void GivenSortedArray_WhenSortedWithMiddlePivot_ThenStaysSorted()
{
var array = QuickSortMethods.CreateSortedArray(100_000);
var expected = QuickSortMethods.CreateSortedArray(100_000);

var sortedArray = sortFunction.SortArray(array, 0, array.Length - 1, string.Empty);
var sortedArray = new QuickSortMethods().SortArrayWithMiddlePivot(array, 0, array.Length - 1);

Assert.IsNotNull(sortedArray);
}
CollectionAssert.AreEqual(expected, sortedArray);
}
}
}
Loading