private static void TestSortAlgorithm( int setLength, int sortAttempts, ISortAlgorithm <IntegerAbstractComparable> sortAlgorithm) { var orderService = new OrderService(); var decisionCounts = new ConcurrentBag <int>(); Parallel.ForEach(Enumerable.Range(0, sortAttempts), attempt => { var integerComparableList = Enumerable.Range(0, setLength) .RandomiseOrder() .Select(i => new IntegerAbstractComparable(i)) .ToArray(); bool completed = false; int decisions = 0; SortResult <IntegerAbstractComparable> orderedIntegerComparableSortResult = null; while (!completed) { orderedIntegerComparableSortResult = orderService .OrderObjects(integerComparableList, sortAlgorithm); completed = orderedIntegerComparableSortResult.Completed; if (!completed) { if (orderedIntegerComparableSortResult.LeftSort.Value > orderedIntegerComparableSortResult.RightSort.Value) { orderedIntegerComparableSortResult.RightSort.AddHigherRankedObject(orderedIntegerComparableSortResult.LeftSort); } else { orderedIntegerComparableSortResult.LeftSort.AddHigherRankedObject(orderedIntegerComparableSortResult.RightSort); } decisions++; } } var expectedIntegerComparableList = integerComparableList.OrderBy(i => i.Value).ToArray(); // asserts Assert.IsTrue(orderedIntegerComparableSortResult.Completed); CollectionAssert.AreEquivalent(expectedIntegerComparableList, orderedIntegerComparableSortResult.SortedResults.ToArray()); decisionCounts.Add(decisions); }); var averageDecision = decisionCounts.Average(); var minDecision = decisionCounts.Min(); var maxDecision = decisionCounts.Max(); var diff = maxDecision - minDecision; Console.WriteLine($"{sortAlgorithm.GetType().Name} average decisions: {averageDecision} minimum decisions: {minDecision} maximum decisions: {maxDecision}"); }
public static void Log(ISortAlgorithm sortAlgorithm, IArrayGenerator arrayGenerator) { var elapsed = RuntimeTimer.Measure(sortAlgorithm); var arrayType = arrayGenerator.GetType().Name; var size = arrayGenerator.Length; var algorithmName = sortAlgorithm.GetType().Name; var benchmarkFileName = $"{algorithmName}_{arrayType}.csv"; var benchmarkDirectory = BenchmarksFolder + $"{algorithmName}/"; Directory.CreateDirectory(benchmarkDirectory); var message = $"Elapsed; {elapsed}; Array Size; {size}; Array Type; {arrayType}; {algorithmName};"; Console.WriteLine(message); using var sw = new StreamWriter(benchmarkDirectory + benchmarkFileName, true); sw.WriteLine(message); }