From 5f3c32420c2a14df59f1f1fae6ff2375cfb56266 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Lema=C5=84czyk?= Date: Fri, 19 Sep 2025 21:41:08 +0200 Subject: [PATCH] Improve results sorting performance --- .../Program.cs | 51 +++++++++---------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/EvenPerfectBitScanner.ResultsParser/Program.cs b/EvenPerfectBitScanner.ResultsParser/Program.cs index 08f99b7..5b1386b 100644 --- a/EvenPerfectBitScanner.ResultsParser/Program.cs +++ b/EvenPerfectBitScanner.ResultsParser/Program.cs @@ -225,9 +225,9 @@ internal static class Program return; } - List sortedResults = [.. primeResults]; - Console.WriteLine("Sorting candidates..."); - BubbleSort(sortedResults); + List sortedResults = [.. primeResults]; + Console.WriteLine("Sorting candidates..."); + SortByPrime(sortedResults); Console.WriteLine("Splitting results..."); List passedResults = new(sortedResults.Count); @@ -323,33 +323,30 @@ internal static class Program } } - private static void BubbleSort(List results) - { - if (results.Count < 2) - { - return; - } + private static void SortByPrime(List results) + { + if (results.Count < 2) + { + return; + } - int length = results.Count; - bool swapped; - do - { - swapped = false; - for (int i = 1; i < length; i++) - { - if (results[i - 1].P <= results[i].P) - { - continue; - } + results.Sort(CandidateResultComparer.Instance); + } - (results[i - 1], results[i]) = (results[i], results[i - 1]); - swapped = true; - } + private sealed class CandidateResultComparer : IComparer + { + public static readonly CandidateResultComparer Instance = new(); - length--; - } - while (swapped); - } + public int Compare(CandidateResult x, CandidateResult y) + { + if (x.P == y.P) + { + return 0; + } + + return x.P < y.P ? -1 : 1; + } + } private static void DrainPrimeMatches( List pendingResults,