3 Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
\r
4 Permission is hereby granted, free of charge, to any person obtaining a copy
\r
5 of this software and associated documentation files (the "Software"), to deal
\r
6 in the Software without restriction, including without limitation the rights
\r
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
8 copies of the Software, and to permit persons to whom the Software is
\r
9 furnished to do so, subject to the following conditions:
\r
11 The above copyright notice and this permission notice shall be included in
\r
12 all copies or substantial portions of the Software.
\r
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
\r
23 using System.Diagnostics;
\r
24 using SCG = System.Collections.Generic;
\r
28 /// A utility class with functions for sorting arrays with respect to an IComparer<T>
\r
30 public class Sorting
\r
35 /// Sort part of array in place using IntroSort
\r
37 /// <exception cref="ArgumentOutOfRangeException">If the <code>start</code>
\r
38 /// and <code>count</code> arguments does not describe a valid range.</exception>
\r
39 /// <param name="array">Array to sort</param>
\r
40 /// <param name="start">Index of first position to sort</param>
\r
41 /// <param name="count">Number of elements to sort</param>
\r
42 /// <param name="comparer">IComparer<T> to sort by</param>
\r
44 public static void IntroSort<T>(T[] array, int start, int count, SCG.IComparer<T> comparer)
\r
46 if (start < 0 || count < 0 || start + count > array.Length)
\r
47 throw new ArgumentOutOfRangeException();
\r
48 new Sorter<T>(array, comparer).IntroSort(start, start + count);
\r
52 /// Sort an array in place using IntroSort and default comparer
\r
54 /// <exception cref="NotComparableException">If T is not comparable</exception>
\r
55 /// <param name="array">Array to sort</param>
\r
57 public static void IntroSort<T>(T[] array)
\r
59 new Sorter<T>(array, Comparer<T>.Default).IntroSort(0, array.Length);
\r
64 /// Sort part of array in place using Insertion Sort
\r
66 /// <exception cref="ArgumentOutOfRangeException">If the <code>start</code>
\r
67 /// and <code>count</code> arguments does not describe a valid range.</exception>
\r
68 /// <param name="array">Array to sort</param>
\r
69 /// <param name="start">Index of first position to sort</param>
\r
70 /// <param name="count">Number of elements to sort</param>
\r
71 /// <param name="comparer">IComparer<T> to sort by</param>
\r
73 public static void InsertionSort<T>(T[] array, int start, int count, SCG.IComparer<T> comparer)
\r
75 if (start < 0 || count < 0 || start + count > array.Length)
\r
76 throw new ArgumentOutOfRangeException();
\r
77 new Sorter<T>(array, comparer).InsertionSort(start, start + count);
\r
82 /// Sort part of array in place using Heap Sort
\r
84 /// <exception cref="ArgumentOutOfRangeException">If the <code>start</code>
\r
85 /// and <code>count</code> arguments does not describe a valid range.</exception>
\r
86 /// <param name="array">Array to sort</param>
\r
87 /// <param name="start">Index of first position to sort</param>
\r
88 /// <param name="count">Number of elements to sort</param>
\r
89 /// <param name="comparer">IComparer<T> to sort by</param>
\r
91 public static void HeapSort<T>(T[] array, int start, int count, SCG.IComparer<T> comparer)
\r
93 if (start < 0 || count < 0 || start + count > array.Length)
\r
94 throw new ArgumentOutOfRangeException();
\r
95 new Sorter<T>(array, comparer).HeapSort(start, start + count);
\r
103 SCG.IComparer<T> c;
\r
106 internal Sorter(T[] a, SCG.IComparer<T> c) { this.a = a; this.c = c; }
\r
109 internal void IntroSort(int f, int b)
\r
113 int depth_limit = (int)Math.Floor(2.5 * Math.Log(b - f, 2));
\r
115 introSort(f, b, depth_limit);
\r
118 InsertionSort(f, b);
\r
122 private void introSort(int f, int b, int depth_limit)
\r
124 const int size_threshold = 14;//24;
\r
126 if (depth_limit-- == 0)
\r
128 else if (b - f <= size_threshold)
\r
129 InsertionSort(f, b);
\r
132 int p = partition(f, b);
\r
134 introSort(f, p, depth_limit);
\r
135 introSort(p, b, depth_limit);
\r
140 private int compare(T i1, T i2) { return c.Compare(i1, i2); }
\r
143 private int partition(int f, int b)
\r
145 int bot = f, mid = (b + f) / 2, top = b - 1;
\r
146 T abot = a[bot], amid = a[mid], atop = a[top];
\r
148 if (compare(abot, amid) < 0)
\r
150 if (compare(atop, abot) < 0)//atop<abot<amid
\r
151 { a[top] = amid; amid = a[mid] = abot; a[bot] = atop; }
\r
152 else if (compare(atop, amid) < 0) //abot<=atop<amid
\r
153 { a[top] = amid; amid = a[mid] = atop; }
\r
154 //else abot<amid<=atop
\r
158 if (compare(amid, atop) > 0) //atop<amid<=abot
\r
159 { a[bot] = atop; a[top] = abot; }
\r
160 else if (compare(abot, atop) > 0) //amid<=atop<abot
\r
161 { a[bot] = amid; amid = a[mid] = atop; a[top] = abot; }
\r
162 else //amid<=abot<=atop
\r
163 { a[bot] = amid; amid = a[mid] = abot; }
\r
166 int i = bot, j = top;
\r
170 while (compare(a[++i], amid) < 0) ;
\r
172 while (compare(amid, a[--j]) < 0) ;
\r
176 T tmp = a[i]; a[i] = a[j]; a[j] = tmp;
\r
184 internal void InsertionSort(int f, int b)
\r
186 for (int j = f + 1; j < b; j++)
\r
188 T key = a[j], other;
\r
191 if (c.Compare(other = a[i], key) > 0)
\r
194 while (i > f && c.Compare(other = a[i - 1], key) > 0) { a[i--] = other; }
\r
202 internal void HeapSort(int f, int b)
\r
204 for (int i = (b + f) / 2; i >= f; i--) heapify(f, b, i);
\r
206 for (int i = b - 1; i > f; i--)
\r
208 T tmp = a[f]; a[f] = a[i]; a[i] = tmp;
\r
214 private void heapify(int f, int b, int i)
\r
216 T pv = a[i], lv, rv, max = pv;
\r
217 int j = i, maxpt = j;
\r
221 int l = 2 * j - f + 1, r = l + 1;
\r
223 if (l < b && compare(lv = a[l], max) > 0) { maxpt = l; max = lv; }
\r
225 if (r < b && compare(rv = a[r], max) > 0) { maxpt = r; max = rv; }
\r