Skip to content
Merged
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
4 changes: 2 additions & 2 deletions csharp-algorithms/BucketSort/BucketSort/BucketSort.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>
50 changes: 50 additions & 0 deletions csharp-algorithms/BucketSort/BucketSort/BucketSortBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using BenchmarkDotNet.Attributes;

namespace BucketSort;

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

[Params(200, 2_000, 20_000)]
public int Size { get; set; }

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

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

[Benchmark]
public int[] SortArray()
{
_source.CopyTo(_work, 0);

return _sorter.SortArray(_work);
}

[Benchmark]
public int[] SortArrayOptimized()
{
_source.CopyTo(_work, 0);

return _sorter.SortArrayOptimized(_work);
}

[Benchmark(Baseline = true)]
public int[] ArraySort()
{
_source.CopyTo(_work, 0);
Array.Sort(_work);

return _work;
}
}
207 changes: 97 additions & 110 deletions csharp-algorithms/BucketSort/BucketSort/BucketSortMethods.cs
Original file line number Diff line number Diff line change
@@ -1,155 +1,142 @@
using BenchmarkDotNet.Attributes;
namespace BucketSort;

namespace BucketSort
public class BucketSortMethods
{
public class BucketSortMethods
public int[] SortArray(int[] array)
{
public IEnumerable<object[]> SampleArrays()
if (array.Length <= 1)
{
yield return new object[] { CreateRandomArray(200), "Small Unsorted" };
yield return new object[] { CreateRandomArray(2000), "Medium Unsorted" };
yield return new object[] { CreateRandomArray(20000), "Large Unsorted" };
yield return new object[] { CreateSortedArray(200), "Small Sorted" };
yield return new object[] { CreateSortedArray(2000), "Medium Sorted" };
yield return new object[] { CreateSortedArray(20000), "Large Sorted" };
return array;
}

[Benchmark]
[ArgumentsSource(nameof(SampleArrays))]
public int[] SortArray(int[] array, string arrayName)
{
List<int> sortedList = new List<int>();
var minValue = array[0];
var maxValue = array[0];

if (array == null || array.Length <= 1)
{
return array;
}
List<int> sortedList = [];
var minValue = array[0];
var maxValue = array[0];

for (int i = 1; i < array.Length; i++)
{
if (array[i] > maxValue)
maxValue = array[i];
if (array[i] < minValue)
minValue = array[i];
}
for (int i = 1; i < array.Length; i++)
{
if (array[i] > maxValue)
maxValue = array[i];
if (array[i] < minValue)
minValue = array[i];
}

var numberOfBuckets = maxValue - minValue + 1;
List<int>[] bucket = new List<int>[numberOfBuckets];
var numberOfBuckets = array.Length;
var range = (long)maxValue - minValue + 1;
List<int>[] bucket = new List<int>[numberOfBuckets];

for (int i = 0; i < numberOfBuckets; i++)
{
bucket[i] = new List<int>();
}

for (int i = 0; i < array.Length; i++)
{
var selectedBucket = (array[i] / numberOfBuckets);
bucket[selectedBucket].Add(array[i]);
}
for (int i = 0; i < numberOfBuckets; i++)
{
bucket[i] = [];
}

for (int i = 0; i < numberOfBuckets; i++)
{
int[] temp = BubbleSort(bucket[i]);
sortedList.AddRange(temp);
}
for (int i = 0; i < array.Length; i++)
{
var selectedBucket = (int)((array[i] - (long)minValue) * numberOfBuckets / range);
bucket[selectedBucket].Add(array[i]);
}

return sortedList.ToArray();
for (int i = 0; i < numberOfBuckets; i++)
{
int[] temp = BubbleSort(bucket[i]);
sortedList.AddRange(temp);
}

public static int[] BubbleSort(List<int> listInput)
return sortedList.ToArray();
}

public static int[] BubbleSort(List<int> listInput)
{
for (int i = 0; i < listInput.Count - 1; i++)
{
for (int i = 0; i < listInput.Count; i++)
for (int j = 0; j < listInput.Count - i - 1; j++)
{
for (int j = 0; j < listInput.Count; j++)
if (listInput[j] > listInput[j + 1])
{
if (listInput[i] < listInput[j])
{
var temp = listInput[i];
listInput[i] = listInput[j];
listInput[j] = temp;
}
var temp = listInput[j];
listInput[j] = listInput[j + 1];
listInput[j + 1] = temp;
}
}
return listInput.ToArray();
}

return listInput.ToArray();
}

[Benchmark]
[ArgumentsSource(nameof(SampleArrays))]
public int[] SortArrayOptimized(int[] array, string arrayName)
public int[] SortArrayOptimized(int[] array)
{
if (array.Length <= 1)
{
if (array == null || array.Length <= 1)
{
return array;
}
return array;
}

int maxValue = array[0];
int minValue = array[0];
int maxValue = array[0];
int minValue = array[0];

for (int i = 1; i < array.Length; i++)
for (int i = 1; i < array.Length; i++)
{
if (array[i] > maxValue)
{
if (array[i] > maxValue)
{
maxValue = array[i];
}
maxValue = array[i];
}

if (array[i] < minValue)
{
minValue = array[i];
}
if (array[i] < minValue)
{
minValue = array[i];
}
}

LinkedList<int>[] bucket = new LinkedList<int>[maxValue - minValue + 1];
var range = (long)maxValue - minValue + 1;
LinkedList<int>[] bucket = new LinkedList<int>[range];

for (int i = 0; i < array.Length; i++)
for (int i = 0; i < array.Length; i++)
{
if (bucket[array[i] - minValue] == null)
{
if (bucket[array[i] - minValue] == null)
{
bucket[array[i] - minValue] = new LinkedList<int>();
}

bucket[array[i] - minValue].AddLast(array[i]);
bucket[array[i] - minValue] = new LinkedList<int>();
}

var index = 0;
for (int i = 0; i < bucket.Length; i++)
bucket[array[i] - minValue].AddLast(array[i]);
}

var index = 0;

for (int i = 0; i < bucket.Length; i++)
{
if (bucket[i] != null)
{
if (bucket[i] != null)
LinkedListNode<int>? node = bucket[i].First;

while (node != null)
{
LinkedListNode<int> node = bucket[i].First;

while (node != null)
{
array[index] = node.Value;
node = node.Next;
index++;
}
array[index] = node.Value;
node = node.Next;
index++;
}
}
return array;
}

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

for (int i = 0; i < size; i++)
array[i] = rand.Next(1, size);
public static int[] CreateRandomArray(int size)
{
var array = new int[size];
var rand = new Random();

return array;
}
for (int i = 0; i < size; i++)
array[i] = rand.Next(1, size);

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

for (int i = 0; i < size; i++)
array[i] = i;
public static int[] CreateSortedArray(int size)
{
var array = new int[size];

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

return array;
}
}
2 changes: 1 addition & 1 deletion csharp-algorithms/BucketSort/BucketSort/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using BenchmarkDotNet.Running;
using BucketSort;

var summary = BenchmarkRunner.Run<BucketSortMethods>();
BenchmarkRunner.Run<BucketSortBenchmarks>(args: args);
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
<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="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<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" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading
Loading