forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RadixSorter.cs
48 lines (44 loc) · 1.5 KB
/
RadixSorter.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
namespace Algorithms.Sorters.Integer;
/// <summary>
/// Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the
/// individual
/// digits which share the same significant position and value. A positional notation is required, but because integers
/// can represent
/// strings of characters (e.g., names or dates) and specially formatted floating point numbers, radix sort is not
/// limited to integers.
/// </summary>
public class RadixSorter : IIntegerSorter
{
/// <summary>
/// Sorts array in ascending order.
/// </summary>
/// <param name="array">Array to sort.</param>
public void Sort(int[] array)
{
var bits = 4;
var b = new int[array.Length];
var rshift = 0;
for (var mask = ~(-1 << bits); mask != 0; mask <<= bits, rshift += bits)
{
var cntarray = new int[1 << bits];
foreach (var t in array)
{
var key = (t & mask) >> rshift;
++cntarray[key];
}
for (var i = 1; i < cntarray.Length; ++i)
{
cntarray[i] += cntarray[i - 1];
}
for (var p = array.Length - 1; p >= 0; --p)
{
var key = (array[p] & mask) >> rshift;
--cntarray[key];
b[cntarray[key]] = array[p];
}
var temp = b;
b = array;
array = temp;
}
}
}