Improve results sorting performance
This commit is contained in:
1 parent
73b5d5d686
commit
5f3c32420c
1 file changed
+13
-16
@@ -227,7 +227,7 @@ internal static class Program
|
|||||||
|
|
||||||
List<CandidateResult> sortedResults = [.. primeResults];
|
List<CandidateResult> sortedResults = [.. primeResults];
|
||||||
Console.WriteLine("Sorting candidates...");
|
Console.WriteLine("Sorting candidates...");
|
||||||
BubbleSort(sortedResults);
|
SortByPrime(sortedResults);
|
||||||
|
|
||||||
Console.WriteLine("Splitting results...");
|
Console.WriteLine("Splitting results...");
|
||||||
List<CandidateResult> passedResults = new(sortedResults.Count);
|
List<CandidateResult> passedResults = new(sortedResults.Count);
|
||||||
@@ -323,32 +323,29 @@ internal static class Program
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void BubbleSort(List<CandidateResult> results)
|
private static void SortByPrime(List<CandidateResult> results)
|
||||||
{
|
{
|
||||||
if (results.Count < 2)
|
if (results.Count < 2)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int length = results.Count;
|
results.Sort(CandidateResultComparer.Instance);
|
||||||
bool swapped;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
swapped = false;
|
|
||||||
for (int i = 1; i < length; i++)
|
|
||||||
{
|
|
||||||
if (results[i - 1].P <= results[i].P)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
(results[i - 1], results[i]) = (results[i], results[i - 1]);
|
private sealed class CandidateResultComparer : IComparer<CandidateResult>
|
||||||
swapped = true;
|
{
|
||||||
|
public static readonly CandidateResultComparer Instance = new();
|
||||||
|
|
||||||
|
public int Compare(CandidateResult x, CandidateResult y)
|
||||||
|
{
|
||||||
|
if (x.P == y.P)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
length--;
|
return x.P < y.P ? -1 : 1;
|
||||||
}
|
}
|
||||||
while (swapped);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void DrainPrimeMatches(
|
private static void DrainPrimeMatches(
|
||||||
|
|||||||
Reference in new issue
Block a user