/// <summary>Buffered list sort.</summary> /// <typeparam name="T">Type of item.</typeparam> /// <param name="list">The list.</param> /// <param name="start">The start.</param> /// <param name="length">The length.</param> /// <param name="comparer">The comparer.</param> private static void BufferedListSort <T>(IList <T> list, int start, int length, Comparison <T> comparer) { Debug.Assert(start >= 0 && start < list.Count); Debug.Assert(length >= 0 && length <= list.Count - start); T[] array = new T[length]; int left, src, dst; if (start == 0 && length >= list.Count) { list.CopyTo(array, 0); // this might be faster than copying one by one } else { left = length; src = start; dst = 0; while (left-- > 0) { array[dst++] = list[src++]; } } ArrayTimSort <T> .Sort(array, comparer); left = length; src = 0; dst = start; while (left-- > 0) { list[dst++] = array[src++]; } }
/// <summary>Sorts the specified array.</summary> /// <typeparam name="T">Type of item.</typeparam> /// <typeparam name="C">Type of compared expression.</typeparam> /// <param name="array">The array.</param> /// <param name="start">The start index.</param> /// <param name="length">The length.</param> /// <param name="expression">The compared expression.</param> public static void TimSort <T, C>(this T[] array, int start, int length, Func <T, C> expression) { length = Math.Min(length, array.Length - start); Comparison <T> comparer = (a, b) => Comparer <C> .Default.Compare(expression(a), expression(b)); ArrayTimSort <T> .Sort(array, start, start + length, comparer); }
public static void Timsort <T> (T[] data, int?offset = null, int?count = null, Comparison <T> comparer = null) { tIndex _offset; tCount _count; if (data == null) { throw new ArgumentNullException("data"); } if (!offset.HasValue) { _offset = 0; } else { _offset = offset.Value; } if (!count.HasValue) { _count = data.Length; } else { _count = count.Value; } if ((_count < 0) || (_offset + _count > data.Length)) { throw new ArgumentException("count"); } if ((_offset < 0) || (_offset > data.Length)) { throw new ArgumentException("offset"); } ArrayTimSort <T> .Sort(data, _offset, _offset + _count, comparer); }
/// <summary>Sorts the specified array.</summary> /// <typeparam name="T">Type of item.</typeparam> /// <param name="array">The array.</param> /// <param name="start">The start index.</param> /// <param name="length">The length.</param> public static void TimSort <T>(this T[] array, int start, int length) { length = Math.Min(length, array.Length - start); ArrayTimSort <T> .Sort(array, start, start + length, Comparer <T> .Default.Compare); }
/// <summary>Sorts the specified array.</summary> /// <typeparam name="T">Type of item.</typeparam> /// <param name="array">The array.</param> public static void TimSort <T>(this T[] array) { ArrayTimSort <T> .Sort(array, Comparer <T> .Default.Compare); }
/// <summary>Sorts the specified array.</summary> /// <typeparam name="T">Type of item.</typeparam> /// <typeparam name="C">Type of compared expression.</typeparam> /// <param name="array">The array.</param> /// <param name="expression">The compared expression.</param> public static void TimSort <T, C>(this T[] array, Func <T, C> expression) { Comparison <T> comparer = (a, b) => Comparer <C> .Default.Compare(expression(a), expression(b)); ArrayTimSort <T> .Sort(array, comparer); }
/// <summary>Sorts the specified array.</summary> /// <typeparam name="T">Type of item.</typeparam> /// <param name="array">The array.</param> /// <param name="comparer">The comparer.</param> public static void TimSort <T>(this T[] array, Comparer <T> comparer) { ArrayTimSort <T> .Sort(array, comparer.Compare); }
/// <summary>Sorts the specified array.</summary> /// <typeparam name="T">Type of item.</typeparam> /// <param name="array">The array.</param> /// <param name="start">The start index.</param> /// <param name="length">The length.</param> /// <param name="comparer">The comparer.</param> public static void TimSort <T>(this T[] array, int start, int length, Comparison <T> comparer) { length = Math.Min(length, array.Length - start); ArrayTimSort <T> .Sort(array, start, start + length, comparer); }