-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortingClass.cs
More file actions
579 lines (502 loc) · 12.9 KB
/
SortingClass.cs
File metadata and controls
579 lines (502 loc) · 12.9 KB
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
using System;
using System.Collections.Generic;
using System.Text;
namespace Algorithms
{
public class SortingClass
{
#region Bubble Sort
/// <summary>
/// Bubble-like swap the largest element to the end of the array at every loop
/// </summary>
/// <param name="array"></param>
public static void BubbleSort(int[] array)
{
int n = array.Length;
for (int i = n - 1; i >= 0; i--)
{
for (int j = 0; j < i; j++)
if (array[j] > array[j + 1])
Swap(ref array[j], ref array[j + 1]);
}
}
#endregion
#region Insertion Sort
/// <summary>
/// Insert the next unsorted element from the unsorted portion into the sorted portion
/// </summary>
/// <param name="array"></param>
public static void InsertionSort(int[] array)
{
int n = array.Length;
int insertVal;
int j;
for (int i = 1; i < n; i++)
{
insertVal = array[i];
j = i;
while (j > 0 && array[j - 1] > insertVal)
{
array[j] = array[j - 1];
j--;
}
array[j] = insertVal;
}
}
#endregion
#region Selection Sort
/// <summary>
/// Select the min element in each loop and place it to the starting index of the loop
/// </summary>
/// <param name="array"></param>
public static void SelectionSort(int[] array)
{
int n = array.Length;
int minIndex;
for (int i = 0; i < n; i++)
{
minIndex = i;
for (int j = i + 1; j < n; j++)
if (array[j] < array[minIndex]) minIndex = j;
if (minIndex != i)
Swap(ref array[i], ref array[minIndex]);
}
}
#endregion
#region Heap Sort
/// <summary>
/// Uses max-heap. First build-max-heap from an unrodered input array, then extract max,
/// and max-heapify.
/// </summary>
/// <param name="array"></param>
public static void HeapSort(int[] array)
{
BuildMaxHeap(array);
for (int i = (array.Length - 1); i > 0; i--)
{
Swap(ref array[i], ref array[0]);
HeapSize--;
MaxHeapify(array, 0);
}
}
private static int HeapSize;
/// <summary>
/// It is assumed that the binary trees rooted at LEFT(i) and RIGHT(i) are max-heaps,
/// but that array[i] may be smaller than its children, thus violating the max-heap
/// property. This function is to let the value at A[i] floats down in the max-heap so
/// that the subtreee rooted at index i becomes a max-heap.
/// </summary>
/// <param name="array"></param>
/// <param name="i"></param>
private static void MaxHeapify(int[] array, int i)
{
int leftIndex = MaxHeapLeft(i);
int rightIndex = MaxHeapRight(i);
int largestIndex;
int n = HeapSize;
if (leftIndex < n && array[leftIndex] > array[i])
largestIndex = leftIndex;
else
largestIndex = i;
if (rightIndex < n && array[rightIndex] > array[largestIndex])
largestIndex = rightIndex;
if (i != largestIndex)
{
Swap(ref array[i], ref array[largestIndex]);
MaxHeapify(array, largestIndex);
}
}
private static void BuildMaxHeap(int[] array)
{
HeapSize = array.Length;
for (int i = (array.Length - 1) / 2; i >= 0; i--)
MaxHeapify(array, i);
}
/// <summary>
/// i is 0 based.
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
private static int MaxHeapParent(int i)
{
return (i + 1) / 2 + 1;
}
private static int MaxHeapLeft(int i)
{
return (i + 1) * 2 - 1;
}
private static int MaxHeapRight(int i)
{
return (i + 1) * 2;
}
#endregion
#region Merge Sort
public static void MergeSort(int[] array)
{
//MergeSortWithOneMoreArray(array);
MergeSortSimplerMain(array);
}
#region Simpler version
public static void MergeSortSimplerMain(int[] array)
{
int[] result = MergeSortSimpler(array);
CopyPartialArray(array, result, 0, array.Length);
}
public static int[] MergeSortSimpler(int[] array)
{
if (array == null || array.Length == 0 || array.Length == 1)
return array;
int mid = (array.Length - 1) / 2;
int[] arrayL = new int[mid + 1];
CopyPartialArray(arrayL, array, 0, mid + 1);
int[] arrayR = new int[array.Length - mid - 1];
CopyPartialArray(arrayR, array, mid + 1, array.Length - mid - 1);
arrayL = MergeSortSimpler(arrayL);
arrayR = MergeSortSimpler(arrayR);
return MergeSimpler(arrayL, arrayR);
}
private static int[] MergeSimpler(int[] arrayL, int[] arrayR)
{
if (arrayL == null || arrayL.Length == 0)
return arrayR;
if (arrayR == null || arrayR.Length == 0)
return arrayL;
int[] result = new int[arrayL.Length + arrayR.Length];
int i = 0;
int j = 0;
int k = 0;
while (i < arrayL.Length && j < arrayR.Length)
{
if (arrayL[i] < arrayR[j])
result[k++] = arrayL[i++];
else
result[k++] = arrayR[j++];
}
while (i < arrayL.Length)
result[k++] = arrayL[i++];
while (j < arrayR.Length)
result[k++] = arrayR[j++];
return result;
}
private static void CopyPartialArray(int[] CopyTo, int[] origArray, int startIndex, int len)
{
for (int i = startIndex, j = 0; i < startIndex + len; i++, j++)
{
CopyTo[j] = origArray[i];
}
}
#endregion
#region With only one additional array
public static void MergeSortWithOneMoreArray(int[] array)
{
int n = array.Length;
int[] workingArray = new int[n];
int s = 1;
while (s < n)
{
MergePass(array, workingArray, s, n);
s += s;
MergePass(workingArray, array, s, n);
s += s;
}
}
/// <summary>
/// Merge two arrays of size s
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="s"></param>
/// <param name="n"></param>
private static void MergePass(int[] x, int[] y, int s, int n)
{
int i = 0;
while (i <= n - 2 * s)
{
Merge(x, y, i, i + s - 1, i + 2 * s - 1);
i = i + 2 * s;
}
if (i + s < n)
Merge(x, y, i, i + s - 1, n - 1);
else
for (int j = i; j < n; j++)
y[j] = x[j];
}
/// <summary>
/// Merge c[l:m] and c[m+1:r] to d[l:r]
/// </summary>
/// <param name="c"></param>
/// <param name="d"></param>
/// <param name="l"></param>
/// <param name="m"></param>
/// <param name="r"></param>
private static void Merge(int[] c, int[] d, int l, int m, int r)
{
int i = l,
j = m + 1,
k = l;
while (i <= m && j <= r)
{
if (c[i] <= c[j])
d[k++] = c[i++];
else
d[k++] = c[j++];
}
if (i > m)
for (int q = j; q <= r; q++)
d[k++] = c[q];
else
for (int q = i; q <= m; q++)
d[k++] = c[q];
}
#endregion
#endregion
#region Quick Sort
public static void QuickSort(int[] array)
{
QuickSortMain(array, 0, array.Length - 1);
}
private static void QuickSortMain(int[] array, int l, int u)
{
if (l < u)
{
int M = QSortPartition(array, l, u);
QuickSortMain(array, l, M - 1);
QuickSortMain(array, M+1, u);
}
}
private static int QSortPartition(int[] array, int l, int u)
{
//Randomly choose an element and swap it with the one with lowest index
Random ran = new Random();
int ranIdx = ran.Next(u - l) + l;
Swap(ref array[l], ref array[ranIdx]);
int M = l;
int val = array[M];
for (int i = l + 1; i <= u; i++)
{
if (array[i] < val)
{
M++;
Swap(ref array[i], ref array[M]);
}
}
Swap(ref array[l], ref array[M]);
return M;
}
#endregion
#region Counting Sort
/// <summary>
/// For each input element x, the functions determins the number of elements
/// less or equal than x. This information can be used to place element x directly
/// into its position in the output array.
/// </summary>
/// <param name="array"></param>
public static void CountingSort(int[] array)
{
int n = array.Length;
int rangeUpperBound = 20;
int[] countsArray = new int[rangeUpperBound + 1];//0...20
int[] sortedArray = new int[array.Length];
for(int i = 0; i <= rangeUpperBound; i++)
countsArray[i] = 0;
for (int i = 0; i < n; i++)
countsArray[array[i]] += 1;
for (int i = 1; i <= rangeUpperBound; i++)
countsArray[i] += countsArray[i - 1];
for (int i = n - 1; i >= 0; i--)
{
sortedArray[countsArray[array[i]]-1] = array[i];
countsArray[array[i]]--;
}
for(int i = 0; i < n; i++)
array[i] = sortedArray[i];
}
#endregion
#region Radix Sort
private class RadixSortElement
{
public int OrigNum;
public int CurrDigit;
public int TotalDigits;
public RadixSortElement(int num)
{
OrigNum = num;
if (OrigNum == 0)
TotalDigits = 1;
else
{
TotalDigits = 0;
while (num != 0)
{
TotalDigits++;
num /= 10;
}
}
SetCurrDigit(1);
}
/// <summary>
///
/// </summary>
/// <param name="digitPlace">starts from 1 for the least significant digit</param>
public void SetCurrDigit(int digitPlace)
{
if (digitPlace < 1)
{
throw new Exception("Incorrect digit place. It must be greater than or equal to 1");
}
if (digitPlace > TotalDigits)
{
CurrDigit = 0;
return;
}
int divider = Convert.ToInt32(Math.Pow(10, digitPlace));
CurrDigit = (OrigNum % divider) / (divider / 10);
}
}
public static void RadixSort(int[] array)
{
int n = array.Length;
int maxDigits = -1;
RadixSortElement[] sortedArray = new RadixSortElement[n];
for (int i = 0; i < n; i++)
{
sortedArray[i] = new RadixSortElement(array[i]);
if (sortedArray[i].TotalDigits > maxDigits)
maxDigits = sortedArray[i].TotalDigits;
}
for (int i = 1; i <= maxDigits; i++)
{
for(int j = 0; j < n; j++)
sortedArray[j].SetCurrDigit(i);
CountingSortForRadixSortElement(sortedArray);
}
for (int i = 0; i < n; i++)
{
array[i] = sortedArray[i].OrigNum;
}
}
private static void CountingSortForRadixSortElement(RadixSortElement[] array)
{
int n = array.Length;
int rangeUpperBound = 10;
int[] countsArray = new int[rangeUpperBound + 1];//0...10
RadixSortElement[] sortedArray = new RadixSortElement[n];
for (int i = 0; i <= rangeUpperBound; i++)
countsArray[i] = 0;
for (int i = 0; i < n; i++)
countsArray[array[i].CurrDigit] += 1;
for (int i = 1; i <= rangeUpperBound; i++)
countsArray[i] += countsArray[i - 1];
for (int i = n - 1; i >= 0; i--)
{
sortedArray[countsArray[array[i].CurrDigit] - 1] = array[i];
countsArray[array[i].CurrDigit]--;
}
for (int i = 0; i < n; i++)
array[i] = sortedArray[i];
}
#endregion
#region Bucket Sort
public static void BucketSort(int[] array)
{
int numOfBuckets = 5;
LinkedList[] buckets = new LinkedList[numOfBuckets];
for (int i = 0; i < numOfBuckets; i++)
buckets[i] = new LinkedList();
int n = array.Length;
for (int i = 0; i < n; i++)
{
int bucketIdx = array[i] / 10;
buckets[bucketIdx].Insert(array[i]);
}
for (int i = 0; i < numOfBuckets; i++)
buckets[i].Sort();
int next = 0;
for (int i = 0; i < numOfBuckets; i++)
{
ListNode curr = buckets[i].Head;
while (curr != null)
{
array[next] = curr.Value;
curr = curr.Next;
next++;
}
}
}
#endregion
#region Bitmap Sort
private static int BITSPERWORD = 32;
private static int BITMAPMAXVAL = 320;
private static int SHIFT = 5; //2 ^ 5 = 32
private static int MASK = 0x1F; //11111 in binary(for the last 5 bits in an integer). 31 in decimal
private static int[] bitmap = new int[BITMAPMAXVAL / BITSPERWORD];
public static void BitmapSort(int[] array)
{
int n = array.Length;
for (int i = 0; i < BITMAPMAXVAL; i++)
clr(i);
for (int i = 0; i < n; i++)
set(array[i]);
int next = 0;
for (int i = 0; i < BITMAPMAXVAL; i++)
{
if (test(i))
{
array[next] = i;
next++;
}
}
}
private static void set(int i)
{
bitmap[i >> SHIFT] |= (1 << (i & MASK));
}
private static void clr(int i)
{
bitmap[i >> SHIFT] &= ~(1 << (i & MASK));
}
private static bool test(int i)
{
return ((bitmap[i >> SHIFT] & (1 << (i & MASK))) != 0);
}
#endregion
#region Helpers
/// <summary>
/// Swap two array elements
/// </summary>
/// <param name="val1"></param>
/// <param name="val2"></param>
private static void Swap(ref int val1, ref int val2)
{
int tmp = val1;
val1 = val2;
val2 = tmp;
}
public static string DisplayArray(int[] array)
{
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < array.Length; i++)
{
strBuilder.Append(array[i].ToString() + " ");
}
return strBuilder.ToString();
}
public static void DisplaySortingResults(int[] origArr, int[] sortedArr, string sortMethod)
{
StringBuilder strBuilder = new StringBuilder();
strBuilder.Append("****" + sortMethod + "****\n");
strBuilder.Append("Original Array : \n");
for (int i = 0; i < origArr.Length; i++)
{
strBuilder.Append(origArr[i].ToString() + " ");
}
strBuilder.Append("\nSorted Array : \n");
for (int j = 0; j < sortedArr.Length; j++)
{
strBuilder.Append(sortedArr[j].ToString() + " ");
}
System.Windows.Forms.MessageBox.Show(strBuilder.ToString());
}
#endregion
}
}