// top-down n-way Merge Sort public static List <T> Sort(List <T> entries, Int32 first, Int32 last) { var length = last + 1 - first; if (length < 2) { return(entries); } if (length < Merges || length < InsertionLimit) { return(InsertionList <T> .Sort(entries, first, last)); } var ranges = new List <List <T> >(Merges); var remaining = length; var mergeSize = length / Merges; for (var left = first; left <= last; left += mergeSize, remaining -= mergeSize) { var rangeSize = Math.Min(mergeSize, remaining); var range = entries.GetRange(left, rangeSize); ranges.Add(Sort(range)); } return(Merge(ranges)); }
public void Sort(T[] entries, Boolean print, UInt32?trials) { if (!trials.HasValue) { trials = 1; } #if TestList var input = entries.ToList(); Header(input, print, typeof(InsertionList <T>)); #else Header(entries, print, typeof(InsertionSort <T>)); #endif Start(); var meter = (IMeter)this; #if !TestRuntimeSort #if TestList var sorter = new InsertionList <T>(meter); #else var sorter = new InsertionSort <T>(meter); #endif #endif for (var trial = 0; trial < trials; trial++) { if (trial > 0) { Reset(); Start(); } #if TestRuntimeSort #if TestList input.Sort(); var output = input; #else Array.Sort(entries); #endif #else #if TestList var output = sorter.Sort(input); #else sorter.Sort(entries); #endif #endif Stop(); Display(); #if TestList Footer(output, print); #else Footer(entries, print); #endif } }