/* Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ using System; using System.Diagnostics; using SCG = System.Collections.Generic; namespace C5 { /// /// A utility class with functions for sorting arrays with respect to an IComparer<T> /// public class Sorting { Sorting() { } /// /// Sort part of array in place using IntroSort /// /// If the start /// and count arguments does not describe a valid range. /// Array to sort /// Index of first position to sort /// Number of elements to sort /// IComparer<T> to sort by [Tested] public static void IntroSort(T[] array, int start, int count, SCG.IComparer comparer) { if (start < 0 || count < 0 || start + count > array.Length) throw new ArgumentOutOfRangeException(); new Sorter(array, comparer).IntroSort(start, start + count); } /// /// Sort an array in place using IntroSort and default comparer /// /// If T is not comparable /// Array to sort [Tested] public static void IntroSort(T[] array) { new Sorter(array, Comparer.Default).IntroSort(0, array.Length); } /// /// Sort part of array in place using Insertion Sort /// /// If the start /// and count arguments does not describe a valid range. /// Array to sort /// Index of first position to sort /// Number of elements to sort /// IComparer<T> to sort by [Tested] public static void InsertionSort(T[] array, int start, int count, SCG.IComparer comparer) { if (start < 0 || count < 0 || start + count > array.Length) throw new ArgumentOutOfRangeException(); new Sorter(array, comparer).InsertionSort(start, start + count); } /// /// Sort part of array in place using Heap Sort /// /// If the start /// and count arguments does not describe a valid range. /// Array to sort /// Index of first position to sort /// Number of elements to sort /// IComparer<T> to sort by [Tested] public static void HeapSort(T[] array, int start, int count, SCG.IComparer comparer) { if (start < 0 || count < 0 || start + count > array.Length) throw new ArgumentOutOfRangeException(); new Sorter(array, comparer).HeapSort(start, start + count); } class Sorter { T[] a; SCG.IComparer c; internal Sorter(T[] a, SCG.IComparer c) { this.a = a; this.c = c; } internal void IntroSort(int f, int b) { if (b - f > 31) { int depth_limit = (int)Math.Floor(2.5 * Math.Log(b - f, 2)); introSort(f, b, depth_limit); } else InsertionSort(f, b); } private void introSort(int f, int b, int depth_limit) { const int size_threshold = 14;//24; if (depth_limit-- == 0) HeapSort(f, b); else if (b - f <= size_threshold) InsertionSort(f, b); else { int p = partition(f, b); introSort(f, p, depth_limit); introSort(p, b, depth_limit); } } private int compare(T i1, T i2) { return c.Compare(i1, i2); } private int partition(int f, int b) { int bot = f, mid = (b + f) / 2, top = b - 1; T abot = a[bot], amid = a[mid], atop = a[top]; if (compare(abot, amid) < 0) { if (compare(atop, abot) < 0)//atop 0) //atop 0) //amid<=atop 0) { a[j] = other; while (i > f && c.Compare(other = a[i - 1], key) > 0) { a[i--] = other; } a[i] = key; } } } internal void HeapSort(int f, int b) { for (int i = (b + f) / 2; i >= f; i--) heapify(f, b, i); for (int i = b - 1; i > f; i--) { T tmp = a[f]; a[f] = a[i]; a[i] = tmp; heapify(f, i, f); } } private void heapify(int f, int b, int i) { T pv = a[i], lv, rv, max = pv; int j = i, maxpt = j; while (true) { int l = 2 * j - f + 1, r = l + 1; if (l < b && compare(lv = a[l], max) > 0) { maxpt = l; max = lv; } if (r < b && compare(rv = a[r], max) > 0) { maxpt = r; max = rv; } if (maxpt == j) break; a[j] = max; max = pv; j = maxpt; } if (j > i) a[j] = pv; } } } }