/
Statics-SortingAlgorithms.cs
390 lines (332 loc) · 13.7 KB
/
Statics-SortingAlgorithms.cs
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
namespace Towel_Testing;
// [TestClass]
public partial class Statics_Testing
{
#region Reverse
[TestMethod]
public void Reverse_Testing()
{
// start < end
for (int size = 1; size < 11; size++)
{
int[] array = (1..(size + 1)).ToArray();
Reverse(0, array.Length - 1, i => array[i], (i, v) => array[i] = v);
Assert.IsTrue(EquateSequence<int>(array, (size..0).ToSpan()));
}
// partial reverse size 3
{
int[] array = (1..4).ToArray();
Reverse(0, 1, i => array[i], (i, v) => array[i] = v);
Assert.IsTrue(EquateSequence<int>(array, stackalloc int[] { 2, 1, 3 }));
}
{
int[] array = (1..4).ToArray();
Reverse(1, 2, i => array[i], (i, v) => array[i] = v);
Assert.IsTrue(EquateSequence<int>(array, stackalloc int[] { 1, 3, 2 }));
}
// partial reverse size 4
{
int[] array = (1..5).ToArray();
Reverse(0, 1, i => array[i], (i, v) => array[i] = v);
Assert.IsTrue(EquateSequence<int>(array, stackalloc int[] { 2, 1, 3, 4 }));
}
{
int[] array = (1..5).ToArray();
Reverse(1, 2, i => array[i], (i, v) => array[i] = v);
Assert.IsTrue(EquateSequence<int>(array, stackalloc int[] { 1, 3, 2, 4 }));
}
{
int[] array = (1..5).ToArray();
Reverse(2, 3, i => array[i], (i, v) => array[i] = v);
Assert.IsTrue(EquateSequence<int>(array, stackalloc int[] { 1, 2, 4, 3 }));
}
{
int[] array = (1..5).ToArray();
Reverse(0, 2, i => array[i], (i, v) => array[i] = v);
Assert.IsTrue(EquateSequence<int>(array, stackalloc int[] { 3, 2, 1, 4 }));
}
{
int[] array = (1..5).ToArray();
Reverse(1, 3, i => array[i], (i, v) => array[i] = v);
Assert.IsTrue(EquateSequence<int>(array, stackalloc int[] { 1, 4, 3, 2 }));
}
// start > end
for (int size = 1; size < 11; size++)
{
int[] array = (1..(size + 1)).ToArray();
Reverse(array.Length - 1, 0, i => array[i], (i, v) => array[i] = v);
Assert.IsTrue(EquateSequence<int>(array, (size..0).ToSpan()));
}
// partial reverse size 3
{
int[] array = (1..4).ToArray();
Reverse(1, 0, i => array[i], (i, v) => array[i] = v);
Assert.IsTrue(EquateSequence<int>(array, stackalloc int[] { 2, 1, 3 }));
}
{
int[] array = (1..4).ToArray();
Reverse(2, 1, i => array[i], (i, v) => array[i] = v);
Assert.IsTrue(EquateSequence<int>(array, stackalloc int[] { 1, 3, 2 }));
}
// partial reverse size 4
{
int[] array = (1..5).ToArray();
Reverse(1, 0, i => array[i], (i, v) => array[i] = v);
Assert.IsTrue(EquateSequence<int>(array, stackalloc int[] { 2, 1, 3, 4 }));
}
{
int[] array = (1..5).ToArray();
Reverse(2, 1, i => array[i], (i, v) => array[i] = v);
Assert.IsTrue(EquateSequence<int>(array, stackalloc int[] { 1, 3, 2, 4 }));
}
{
int[] array = (1..5).ToArray();
Reverse(3, 2, i => array[i], (i, v) => array[i] = v);
Assert.IsTrue(EquateSequence<int>(array, stackalloc int[] { 1, 2, 4, 3 }));
}
{
int[] array = (1..5).ToArray();
Reverse(2, 0, i => array[i], (i, v) => array[i] = v);
Assert.IsTrue(EquateSequence<int>(array, stackalloc int[] { 3, 2, 1, 4 }));
}
{
int[] array = (1..5).ToArray();
Reverse(3, 1, i => array[i], (i, v) => array[i] = v);
Assert.IsTrue(EquateSequence<int>(array, stackalloc int[] { 1, 4, 3, 2 }));
}
}
#endregion
#region Shuffle
[TestMethod]
public void Shuffle_Testing()
{
Random random = new(7);
int[] array = (..10).ToArray();
Shuffle<int>(array, random);
Assert.IsFalse(IsOrdered<int>(array));
}
#endregion
#region Sorting Algorithms
internal static void TestSortAlgorithm(
Action<int, int, int[]> algorithm,
int? maxSize = null,
bool negatives = true)
{
// Test sorting the full array (0...length - 1)
foreach (int size in (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 100, 101, 1000, 1001))
{
if (maxSize is null || maxSize.Value >= size)
{
Random random = new(7);
int[] array = (size..0).ToArray();
if (size > 1)
{
Shuffle<int>(array, random);
if (IsOrdered<int>(array))
{
Array.Reverse(array);
}
}
algorithm(0, array.Length - 1, array);
Assert.IsTrue(IsOrdered<int>(array));
Assert.IsTrue(EquateSequence<int>(array, (1..(size + 1)).ToSpan()));
}
}
// Test sorting the full array (0...length - 1) with negative values
if (negatives)
{
foreach (int size in (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 100, 101, 1000, 1001))
{
if (maxSize is null || maxSize.Value >= size)
{
Random random = new(7);
int[] array = (size..0).ToArray(i => i - size);
if (size > 1)
{
Shuffle<int>(array, random);
if (IsOrdered<int>(array))
{
Array.Reverse(array);
}
}
algorithm(0, array.Length - 1, array);
Assert.IsTrue(IsOrdered<int>(array));
var tmp = (0..size).ToSpan(i => i - size + 1);
Assert.IsTrue(EquateSequence<int>(array, (0..size).ToSpan(i => i - size + 1)));
}
}
}
// Test sorting the full array (0...length - 1) with randomized values
foreach (int size in (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 100, 101, 1000, 1001))
{
if (maxSize is null || maxSize.Value >= size)
{
Random random = new(7);
int[] array = (size..0).ToArray(i => random.Next(1, 100000));
if (size > 1)
{
Shuffle<int>(array, random);
if (IsOrdered<int>(array))
{
Array.Reverse(array);
}
}
algorithm(0, array.Length - 1, array);
Assert.IsTrue(IsOrdered<int>(array));
}
}
// Test sorting the full array (0...length - 1) with randomized negative values
if (negatives)
{
foreach (int size in (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 100, 101, 1000, 1001))
{
if (maxSize is null || maxSize.Value >= size)
{
Random random = new(7);
int[] array = (size..0).ToArray(i => random.Next(1, 100000) - 100000);
if (size > 1)
{
Shuffle<int>(array, random);
if (IsOrdered<int>(array))
{
Array.Reverse(array);
}
}
algorithm(0, array.Length - 1, array);
Assert.IsTrue(IsOrdered<int>(array));
}
}
}
// Test sorting the full array (0...length - 1) with randomized negative and positive values
if (negatives)
{
foreach (int size in (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 100, 101, 1000, 1001))
{
if (maxSize is null || maxSize.Value >= size)
{
Random random = new(7);
int[] array = (size..0).ToArray(i => random.Next(1, 100000) - 50000);
if (size > 1)
{
Shuffle<int>(array, random);
if (IsOrdered<int>(array))
{
Array.Reverse(array);
}
}
algorithm(0, array.Length - 1, array);
Assert.IsTrue(IsOrdered<int>(array));
}
}
}
// Test partial sorting of the array (start > 0 && end < length - 1)
if (maxSize is null || maxSize.Value >= 5)
{
int[] array = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
algorithm(3, 7, array);
Span<int> expected = stackalloc int[] { 9, 8, 7, /*<*/ 2, 3, 4, 5, 6, /*>*/ 1, 0 };
Assert.IsTrue(EquateSequence<int>(array, expected));
}
if (maxSize is null || maxSize.Value >= 5)
{
int[] array = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
algorithm(4, 8, array);
Span<int> expected = stackalloc int[] { 10, 9, 8, 7, /*<*/ 2, 3, 4, 5, 6, /*>*/ 1, 0 };
Assert.IsTrue(EquateSequence<int>(array, expected));
}
if (maxSize is null || maxSize.Value >= 980)
{
Random random = new(7);
int[] array = (1000..0).ToArray();
Shuffle<int>(array.AsSpan(10..990), random);
algorithm(10, 990, array);
Span<int> expected = ArrayHelper.NewFromRanges(1000..990, 10..991, 9..0);
Assert.IsTrue(EquateSequence<int>(array, expected));
}
}
[TestMethod]
public void Bubble_Testing() => TestSortAlgorithm((start, end, array) => SortBubble(start, end, i => array[i], (i, v) => array[i] = v));
[TestMethod]
public void Bubble_Span_Testing() => TestSortAlgorithm((start, end, array) => SortBubble(array.AsSpan()[start..(end + 1)]));
[TestMethod]
public void Insertion_Testing() => TestSortAlgorithm((start, end, array) => SortInsertion(start, end, i => array[i], (i, v) => array[i] = v));
[TestMethod]
public void Insertion_Span_Testing() => TestSortAlgorithm((start, end, array) => SortInsertion(array.AsSpan()[start..(end + 1)]));
[TestMethod]
public void Selection_Testing() => TestSortAlgorithm((start, end, array) => SortSelection(start, end, i => array[i], (i, v) => array[i] = v));
[TestMethod]
public void Selection_Span_Testing() => TestSortAlgorithm((start, end, array) => SortSelection(array.AsSpan()[start..(end + 1)]));
[TestMethod]
public void Merge_Testing() => TestSortAlgorithm((start, end, array) => SortMerge(start, end, i => array[i], (i, v) => array[i] = v));
[TestMethod]
public void Merge_Span_Testing() => TestSortAlgorithm((start, end, array) => SortMerge(array.AsSpan()[start..(end + 1)]));
[TestMethod]
public void Quick_Testing() => TestSortAlgorithm((start, end, array) => SortQuick(start, end, i => array[i], (i, v) => array[i] = v));
[TestMethod]
public void Quick_Span_Testing() => TestSortAlgorithm((start, end, array) => SortQuick(array.AsSpan()[start..(end + 1)]));
[TestMethod]
public void Heap_Testing() => TestSortAlgorithm((start, end, array) => SortHeap(start, end, i => array[i], (i, v) => array[i] = v));
[TestMethod]
public void Heap_Span_Testing() => TestSortAlgorithm((start, end, array) => SortHeap(array.AsSpan()[start..(end + 1)]));
[TestMethod]
public void OddEven_Testing() => TestSortAlgorithm((start, end, array) => SortOddEven(start, end, i => array[i], (i, v) => array[i] = v));
[TestMethod]
public void OddEven_Span_Testing() => TestSortAlgorithm((start, end, array) => SortOddEven(array.AsSpan()[start..(end + 1)]));
[TestMethod]
public void Slow_Testing() => TestSortAlgorithm((start, end, array) => SortSlow(start, end, i => array[i], (i, v) => array[i] = v), 11);
[TestMethod]
public void Slow_Span_Testing() => TestSortAlgorithm((start, end, array) => SortSlow(array.AsSpan()[start..(end + 1)]), 11);
[TestMethod]
public void Gnome_Testing() => TestSortAlgorithm((start, end, array) => SortGnome(start, end, i => array[i], (i, v) => array[i] = v));
[TestMethod]
public void Gnome_Span_Testing() => TestSortAlgorithm((start, end, array) => SortGnome(array.AsSpan()[start..(end + 1)]));
[TestMethod]
public void Comb_Testing() => TestSortAlgorithm((start, end, array) => SortComb(start, end, i => array[i], (i, v) => array[i] = v));
[TestMethod]
public void Comb_Span_Testing() => TestSortAlgorithm((start, end, array) => SortComb(array.AsSpan()[start..(end + 1)]));
[TestMethod]
public void Shell_Testing() => TestSortAlgorithm((start, end, array) => SortShell(start, end, i => array[i], (i, v) => array[i] = v));
[TestMethod]
public void Shell_Span_Testing() => TestSortAlgorithm((start, end, array) => SortShell(array.AsSpan()[start..(end + 1)]));
[TestMethod]
public void Cocktail_Testing() => TestSortAlgorithm((start, end, array) => SortCocktail(start, end, i => array[i], (i, v) => array[i] = v));
[TestMethod]
public void Cocktail_Span_Testing() => TestSortAlgorithm((start, end, array) => SortCocktail(array.AsSpan()[start..(end + 1)]));
[TestMethod]
public void Cycle_Testing() => TestSortAlgorithm((start, end, array) => SortCycle(start, end, i => array[i], (i, v) => array[i] = v));
[TestMethod]
public void Cycle_Span_Testing() => TestSortAlgorithm((start, end, array) => SortCycle(array.AsSpan()[start..(end + 1)]));
[TestMethod]
public void Pancake_Testing() => TestSortAlgorithm((start, end, array) => SortPancake(start, end, i => array[i], (i, v) => array[i] = v));
[TestMethod]
public void Pancake_Span_Testing() => TestSortAlgorithm((start, end, array) => SortPancake(array.AsSpan()[start..(end + 1)]));
[TestMethod]
public void Stooge_Testing() => TestSortAlgorithm((start, end, array) => SortStooge(start, end, i => array[i], (i, v) => array[i] = v), maxSize: 100);
[TestMethod]
public void Stooge_Span_Testing() => TestSortAlgorithm((start, end, array) => SortStooge(array.AsSpan()[start..(end + 1)]), maxSize: 100);
[TestMethod]
public void Bogo_Testing() => TestSortAlgorithm((start, end, array) => SortBogo(start, end, i => array[i], (i, v) => array[i] = v), maxSize: 6);
[TestMethod]
public void Bogo_Span_Testing() => TestSortAlgorithm((start, end, array) => SortBogo(array.AsSpan()[start..(end + 1)]), maxSize: 6);
[TestMethod]
public void Tim_Testing() => TestSortAlgorithm((start, end, array) => SortTim(start, end, i => array[i], (i, v) => array[i] = v));
[TestMethod]
public void Tim_Span_Testing() => TestSortAlgorithm((start, end, array) => SortTim(array.AsSpan()[start..(end + 1)]));
[TestMethod]
public void Counting_Testing() => TestSortAlgorithm((start, end, array) => SortCounting(start, end, i => (uint)i, i => array[i], (i, v) => array[i] = v), negatives: false);
[TestMethod]
public void Counting_Span_Testing() => TestSortAlgorithm((start, end, array) => SortCounting<int>(array.AsSpan()[start..(end + 1)], i => (uint)i), negatives: false);
[TestMethod]
public void Radix_Testing() => TestSortAlgorithm((start, end, array) => SortRadix(start, end, i => (uint)i, i => array[i], (i, v) => array[i] = v), negatives: false);
[TestMethod]
public void Radix_Span_Testing() => TestSortAlgorithm((start, end, array) => SortRadix<int>(array.AsSpan()[start..(end + 1)], i => (uint)i), negatives: false);
[TestMethod]
public void PidgeonHole_Testing() => TestSortAlgorithm((start, end, array) => SortPidgeonHole(start, end, i => array[i], (i, v) => array[i] = v));
[TestMethod]
public void PidgeonHole_Span_Testing() => TestSortAlgorithm((start, end, array) => SortPidgeonHole(array.AsSpan()[start..(end + 1)]));
[TestMethod]
public void Intro_Testing() => TestSortAlgorithm((start, end, array) => SortIntro(start, end, i => array[i], (i, v) => array[i] = v));
[TestMethod]
public void Intro_Span_Testing() => TestSortAlgorithm((start, end, array) => SortIntro(array.AsSpan()[start..(end + 1)]));
#endregion
}