5 // Alejandro Serrano "Serras" (trupill@yahoo.es)
6 // Marek Safar <marek.safar@gmail.com>
7 // Jb Evain (jbevain@novell.com)
9 // (C) 2007 - 2008 Novell, Inc. (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections.Generic;
34 namespace System.Linq {
36 class QuickSort<TElement> {
40 SortContext<TElement> context;
42 QuickSort (IEnumerable<TElement> source, SortContext<TElement> context)
44 this.elements = source.ToArray ();
45 this.indexes = CreateIndexes (elements.Length);
46 this.context = context;
49 static int [] CreateIndexes (int length)
51 var indexes = new int [length];
52 for (int i = 0; i < length; i++)
60 // If the source contains just zero or one element, there's no need to sort
61 if (elements.Length <= 1)
64 context.Initialize (elements);
66 // Then sorts the elements according to the collected
67 // key values and the selected ordering
68 Sort (0, indexes.Length - 1);
71 int CompareItems (int first_index, int second_index)
73 return context.Compare (first_index, second_index);
76 // We look at the first, middle, and last items in the subarray.
77 // Then we put the largest on the right side, the smallest on
78 // the left side, and the median becomes our pivot.
79 int MedianOfThree (int left, int right)
81 int center = (left + right) / 2;
82 if (CompareItems (indexes [center], indexes [left]) < 0)
84 if (CompareItems (indexes [right], indexes [left]) < 0)
86 if (CompareItems (indexes [right], indexes [center]) < 0)
88 Swap (center, right - 1);
89 return indexes [right - 1];
92 void Sort (int left, int right)
94 if (left + 3 <= right) {
95 int l = left, r = right - 1, pivot = MedianOfThree (left, right);
97 while (CompareItems (indexes [++l], pivot) < 0) { }
98 while (CompareItems (indexes [--r], pivot) > 0) { }
107 // Partition and sort
111 // If there are three items in the subarray, insertion sort is better
112 InsertionSort (left, right);
115 void InsertionSort (int left, int right)
117 for (int i = left + 1; i <= right; i++) {
118 int j, tmp = indexes [i];
120 for (j = i; j > left && CompareItems (tmp, indexes [j - 1]) < 0; j--)
121 indexes [j] = indexes [j - 1];
127 void Swap (int left, int right)
129 int temp = indexes [right];
130 indexes [right] = indexes [left];
131 indexes [left] = temp;
134 public static IEnumerable<TElement> Sort (IEnumerable<TElement> source, SortContext<TElement> context)
136 var sorter = new QuickSort<TElement> (source, context);
138 sorter.PerformSort ();
140 for (int i = 0; i < sorter.indexes.Length; i++)
141 yield return sorter.elements [sorter.indexes [i]];