forked from aalhour/C-Sharp-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LSDRadixSorter.cs
104 lines (83 loc) · 3.24 KB
/
LSDRadixSorter.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
using System;
using System.Collections.Generic;
namespace Algorithms.Sorting
{
/// <summary>
/// LSD (Least Significat Digit) Radix Sort.
///
/// Sorts ASCII-encoded strings.
/// Implemented as a static class of extension methods.
/// </summary>
public static class LSDRadixSorter
{
/// <summary>
/// Extension method for sorting strings.
/// </summary>
public static string LSDRadixSort(this string source)
{
if (string.IsNullOrEmpty(source) || source.Length <= 1)
return source;
// LSD Radix Sort the character arrapy representation of the string
var charArray = source.ToCharArray();
charArray.LSDRadixSort();
return new String(charArray);
}
/// <summary>
/// Extension method for sorting character arrays, in place.
/// </summary>
public static void LSDRadixSort(this char[] source)
{
if (source == null || source.Length <= 1)
return;
// extend ASCII alphabet size
int asciiSize = 256;
int length = source.Length;
char[] auxiliary = new char[length];
// compute frequency counts
int[] count = new int[asciiSize + 1];
for (int i = 0; i < length; i++)
count[source[i] + 1]++;
// compute cumulates
for (int r = 0; r < asciiSize; r++)
count[r + 1] += count[r];
// move data
for (int i = 0; i < length; i++)
auxiliary[count[source[i]]++] = source[i];
// copy back
for (int i = 0; i < length; i++)
source[i] = auxiliary[i];
}
/// <summary>
/// Extension method for sorting collections of strings of the same width, in place.
/// </summary>
public static void LSDRadixSort(this IList<String> collection, int stringFixedWidth)
{
// Validate input
if (collection == null || collection.Count <= 1)
return;
for (int i = 0; i < collection.Count; ++i)
if (collection[i] == null || collection[i].Length != stringFixedWidth)
throw new InvalidOperationException("Not all strings have the same width");
// extend ASCII alphabet size
int asciiSize = 256;
int stringsCount = collection.Count;
string[] auxiliary = new string[stringsCount];
for (int d = stringFixedWidth - 1; d >= 0; d--)
{
// compute frequency counts
int[] count = new int[asciiSize + 1];
for (int i = 0; i < stringsCount; i++)
count[collection[i][d] + 1]++;
// compute cumulates
for (int r = 0; r < asciiSize; r++)
count[r + 1] += count[r];
// move data
for (int i = 0; i < stringsCount; i++)
auxiliary[count[collection[i][d]]++] = collection[i];
// copy back
for (int i = 0; i < stringsCount; i++)
collection[i] = auxiliary[i];
}
}
}
}