- Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathLSDRadixSorter.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
usingSystem;
usingSystem.Collections.Generic;
namespaceAlgorithms.Sorting
{
/// <summary>
/// LSD (Least Significat Digit) Radix Sort.
///
/// Sorts ASCII-encoded strings.
/// Implemented as a static class of extension methods.
/// </summary>
publicstaticclassLSDRadixSorter
{
/// <summary>
/// Extension method for sorting strings.
/// </summary>
publicstaticstringLSDRadixSort(thisstringsource)
{
if(string.IsNullOrEmpty(source)||source.Length<=1)
returnsource;
// LSD Radix Sort the character arrapy representation of the string
varcharArray=source.ToCharArray();
charArray.LSDRadixSort();
returnnewString(charArray);
}
/// <summary>
/// Extension method for sorting character arrays, in place.
/// </summary>
publicstaticvoidLSDRadixSort(thischar[]source)
{
if(source==null||source.Length<=1)
return;
// extend ASCII alphabet size
intasciiSize=256;
intlength=source.Length;
char[]auxiliary=newchar[length];
// compute frequency counts
int[]count=newint[asciiSize+1];
for(inti=0;i<length;i++)
count[source[i]+1]++;
// compute cumulates
for(intr=0;r<asciiSize;r++)
count[r+1]+=count[r];
// move data
for(inti=0;i<length;i++)
auxiliary[count[source[i]]++]=source[i];
// copy back
for(inti=0;i<length;i++)
source[i]=auxiliary[i];
}
/// <summary>
/// Extension method for sorting collections of strings of the same width, in place.
/// </summary>
publicstaticvoidLSDRadixSort(thisIList<String>collection,intstringFixedWidth)
{
// Validate input
if(collection==null||collection.Count<=1)
return;
for(inti=0;i<collection.Count;++i)
if(collection[i]==null||collection[i].Length!=stringFixedWidth)
thrownewInvalidOperationException("Not all strings have the same width");
// extend ASCII alphabet size
intasciiSize=256;
intstringsCount=collection.Count;
string[]auxiliary=newstring[stringsCount];
for(intd=stringFixedWidth-1;d>=0;d--)
{
// compute frequency counts
int[]count=newint[asciiSize+1];
for(inti=0;i<stringsCount;i++)
count[collection[i][d]+1]++;
// compute cumulates
for(intr=0;r<asciiSize;r++)
count[r+1]+=count[r];
// move data
for(inti=0;i<stringsCount;i++)
auxiliary[count[collection[i][d]]++]=collection[i];
// copy back
for(inti=0;i<stringsCount;i++)
collection[i]=auxiliary[i];
}
}
}
}