Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Sorting.cs « C5 « Mono.C5 « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c30b8a7b4888f0c5c58c1f5ba7c4be97eb8f243d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#if NET_2_0
/*
 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
{
  /// <summary>
  /// A utility class with functions for sorting arrays with respect to an IComparer&lt;T&gt;
  /// </summary>
  public class Sorting
  {
    Sorting() { }

    /// <summary>
    /// Sort part of array in place using IntroSort
    /// </summary>
    /// <exception cref="ArgumentOutOfRangeException">If the <code>start</code>
    /// and <code>count</code> arguments does not describe a valid range.</exception>
    /// <param name="array">Array to sort</param>
    /// <param name="start">Index of first position to sort</param>
    /// <param name="count">Number of elements to sort</param>
    /// <param name="comparer">IComparer&lt;T&gt; to sort by</param>
    [Tested]
    public static void IntroSort<T>(T[] array, int start, int count, SCG.IComparer<T> comparer)
    {
      if (start < 0 || count < 0 || start + count > array.Length)
        throw new ArgumentOutOfRangeException();
      new Sorter<T>(array, comparer).IntroSort(start, start + count);
    }

    /// <summary>
    /// Sort an array in place using IntroSort and default comparer
    /// </summary>
    /// <exception cref="NotComparableException">If T is not comparable</exception>
    /// <param name="array">Array to sort</param>
    [Tested]
    public static void IntroSort<T>(T[] array)
    {
      new Sorter<T>(array, Comparer<T>.Default).IntroSort(0, array.Length);
    }


    /// <summary>
    /// Sort part of array in place using Insertion Sort
    /// </summary>
    /// <exception cref="ArgumentOutOfRangeException">If the <code>start</code>
    /// and <code>count</code> arguments does not describe a valid range.</exception>
    /// <param name="array">Array to sort</param>
    /// <param name="start">Index of first position to sort</param>
    /// <param name="count">Number of elements to sort</param>
    /// <param name="comparer">IComparer&lt;T&gt; to sort by</param>
    [Tested]
    public static void InsertionSort<T>(T[] array, int start, int count, SCG.IComparer<T> comparer)
    {
      if (start < 0 || count < 0 || start + count > array.Length)
        throw new ArgumentOutOfRangeException();
      new Sorter<T>(array, comparer).InsertionSort(start, start + count);
    }


    /// <summary>
    /// Sort part of array in place using Heap Sort
    /// </summary>
    /// <exception cref="ArgumentOutOfRangeException">If the <code>start</code>
    /// and <code>count</code> arguments does not describe a valid range.</exception>
    /// <param name="array">Array to sort</param>
    /// <param name="start">Index of first position to sort</param>
    /// <param name="count">Number of elements to sort</param>
    /// <param name="comparer">IComparer&lt;T&gt; to sort by</param>
    [Tested]
    public static void HeapSort<T>(T[] array, int start, int count, SCG.IComparer<T> comparer)
    {
      if (start < 0 || count < 0 || start + count > array.Length)
        throw new ArgumentOutOfRangeException();
      new Sorter<T>(array, comparer).HeapSort(start, start + count);
    }


    class Sorter<T>
    {
      T[] a;

      SCG.IComparer<T> c;


      internal Sorter(T[] a, SCG.IComparer<T> 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<abot<amid
          { a[top] = amid; amid = a[mid] = abot; a[bot] = atop; }
          else if (compare(atop, amid) < 0) //abot<=atop<amid
          { a[top] = amid; amid = a[mid] = atop; }
          //else abot<amid<=atop
        }
        else
        {
          if (compare(amid, atop) > 0) //atop<amid<=abot
          { a[bot] = atop; a[top] = abot; }
          else if (compare(abot, atop) > 0) //amid<=atop<abot
          { a[bot] = amid; amid = a[mid] = atop; a[top] = abot; }
          else //amid<=abot<=atop
          { a[bot] = amid; amid = a[mid] = abot; }
        }

        int i = bot, j = top;

        while (true)
        {
          while (compare(a[++i], amid) < 0) ;

          while (compare(amid, a[--j]) < 0) ;

          if (i < j)
          {
            T tmp = a[i]; a[i] = a[j]; a[j] = tmp;
          }
          else
            return i;
        }
      }


      internal void InsertionSort(int f, int b)
      {
        for (int j = f + 1; j < b; j++)
        {
          T key = a[j], other;
          int i = j - 1;

          if (c.Compare(other = a[i], key) > 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;
      }
    }
  }
}
#endif