Skip to content

Commit 87fec92

Browse files
authored
Merge pull request #8 from guitarrapc/flaststablesort
feat: add FastStableSort
2 parents 69838ee + 33e7af1 commit 87fec92

7 files changed

Lines changed: 2005 additions & 3 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#:sdk Microsoft.NET.Sdk
2+
#:property TargetFramework=net10.0
3+
#:project ../../src/SortAlgorithm
4+
using SortAlgorithm.Algorithms;
5+
using SortAlgorithm.Contexts;
6+
7+
foreach (var n in new[] { 17, 20, 50, 100 })
8+
{
9+
Console.WriteLine($"=== n={n} ===");
10+
11+
// Sorted
12+
{
13+
var stats = new StatisticsContext();
14+
var sorted = Enumerable.Range(0, n).ToArray();
15+
FlashSort.Sort(sorted.AsSpan(), stats);
16+
Console.WriteLine($" Sorted : Compares={stats.CompareCount}, Writes={stats.IndexWriteCount}, Reads={stats.IndexReadCount}, Swaps={stats.SwapCount}");
17+
}
18+
19+
// Reversed
20+
{
21+
var stats = new StatisticsContext();
22+
var reversed = Enumerable.Range(0, n).Reverse().ToArray();
23+
FlashSort.Sort(reversed.AsSpan(), stats);
24+
Console.WriteLine($" Reversed : Compares={stats.CompareCount}, Writes={stats.IndexWriteCount}, Reads={stats.IndexReadCount}, Swaps={stats.SwapCount}");
25+
}
26+
27+
// Random (multiple runs)
28+
for (var seed = 0; seed < 3; seed++)
29+
{
30+
var stats = new StatisticsContext();
31+
var random = Enumerable.Range(0, n).OrderBy(_ => seed * 1000 + Guid.NewGuid().GetHashCode()).ToArray();
32+
FlashSort.Sort(random.AsSpan(), stats);
33+
Console.WriteLine($" Random{seed} : Compares={stats.CompareCount}, Writes={stats.IndexWriteCount}, Reads={stats.IndexReadCount}, Swaps={stats.SwapCount}");
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#:sdk Microsoft.NET.Sdk
2+
#:property TargetFramework=net10.0
3+
#:project ../../src/SortAlgorithm
4+
using SortAlgorithm.Algorithms;
5+
using SortAlgorithm.Contexts;
6+
7+
foreach (var n in new[] { 10, 20, 50, 100 })
8+
{
9+
Console.WriteLine($"=== n={n} ===");
10+
11+
// Sorted
12+
{
13+
var stats = new StatisticsContext();
14+
var sorted = Enumerable.Range(0, n).ToArray();
15+
FlatStableSort.Sort(sorted.AsSpan(), stats);
16+
Console.WriteLine($" Sorted : Compares={stats.CompareCount}, Writes={stats.IndexWriteCount}, Reads={stats.IndexReadCount}, Swaps={stats.SwapCount}");
17+
}
18+
19+
// Reversed
20+
{
21+
var stats = new StatisticsContext();
22+
var reversed = Enumerable.Range(0, n).Reverse().ToArray();
23+
FlatStableSort.Sort(reversed.AsSpan(), stats);
24+
Console.WriteLine($" Reversed : Compares={stats.CompareCount}, Writes={stats.IndexWriteCount}, Reads={stats.IndexReadCount}, Swaps={stats.SwapCount}");
25+
}
26+
27+
// Random (multiple runs)
28+
for (var seed = 0; seed < 3; seed++)
29+
{
30+
var stats = new StatisticsContext();
31+
var random = Enumerable.Range(0, n).OrderBy(_ => seed * 1000 + Guid.NewGuid().GetHashCode()).ToArray();
32+
FlatStableSort.Sort(random.AsSpan(), stats);
33+
Console.WriteLine($" Random{seed} : Compares={stats.CompareCount}, Writes={stats.IndexWriteCount}, Reads={stats.IndexReadCount}, Swaps={stats.SwapCount}");
34+
}
35+
}

src/SortAlgorithm.Benchmark/MergeBenchmark.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class MergeBenchmark
2525
private int[] _spinvariantArray = default!;
2626
private int[] _spinArray = default!;
2727
private int[] _glidesortArray = default!;
28+
private int[] _flatstableArray = default!;
2829

2930
[IterationSetup]
3031
public void Setup()
@@ -44,6 +45,7 @@ public void Setup()
4445
_spinvariantArray = BenchmarkData.GenerateIntArray(Size, Pattern);
4546
_spinArray = BenchmarkData.GenerateIntArray(Size, Pattern);
4647
_glidesortArray = BenchmarkData.GenerateIntArray(Size, Pattern);
48+
_flatstableArray = BenchmarkData.GenerateIntArray(Size, Pattern);
4749
}
4850

4951
[Benchmark(Baseline = true)]
@@ -135,4 +137,10 @@ public void Glidesort()
135137
{
136138
SortAlgorithm.Algorithms.Glidesort.Sort(_glidesortArray.AsSpan());
137139
}
140+
141+
[Benchmark]
142+
public void FlatStableSort()
143+
{
144+
SortAlgorithm.Algorithms.FlatStableSort.Sort(_flatstableArray.AsSpan());
145+
}
138146
}

0 commit comments

Comments
 (0)