Not sure why i’d be getting such odd numbers when looking at the number of calls to the Comparer.
For 2 strings: 5 calls?
Plus there are sequences in there like this
12 Strings: 66 calls
13 Strings: 85 calls
14 Strigns: 91 calls
15 Strings: 89 calls ?????
Could it really be more efficient to sort 15 strings than 14?
int Iterations = 20;
int LastCycle = 0;
int CallsToSort = 0;
while (Iterations > 0)
{
LastCycle = CallsToSort;
CallsToSort = 0;
var strings = new string[Iterations];
for (int i = 0; i < Iterations; i++) { strings[i] = "test" + i; }
Array.Sort(strings, (s1, s2) => { CallsToSort++; return s1.CompareTo(s2); });
Console.WriteLine("Strings:{0}\nCalls to Sort: {1}\n\t\tDiff:{2}\n\n", Iterations, CallsToSort, LastCycle-CallsToSort);
Iterations--;
}
The
Array.Sort()method will (usually) end up using a version of QuickSort where the pivots are chosen deterministically. The number of comparisons QuickSort uses will depend heavily on the pivots, and you are seeing that in your results.Some code (from ILSpy):
That
tis the pivot. You can probably work out exactly why you see the results from there.