forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountingSorter.cs
58 lines (52 loc) · 2.03 KB
/
CountingSorter.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
using System;
using System.Linq;
namespace Algorithms.Sorters.Integer;
/// <summary>
/// Counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that
/// is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key
/// value, and using arithmetic on those counts to determine the positions of each key value in the output sequence.
/// Its running time is linear in the number of items and the difference between the maximum and minimum key values, so
/// it is only suitable for direct use in situations where the variation in keys is not significantly greater than the
/// number of items. However, it is often used as a subroutine in another sorting algorithm, radix sort, that can
/// handle larger keys more efficiently.
/// </summary>
public class CountingSorter : IIntegerSorter
{
/// <summary>
/// <para>
/// Sorts input array using counting sort algorithm.
/// </para>
/// <para>
/// Time complexity: O(n+k), where k is the range of the non-negative key values.
/// </para>
/// <para>
/// Space complexity: O(n+k), where k is the range of the non-negative key values.
/// </para>
/// </summary>
/// <param name="array">Input array.</param>
public void Sort(int[] array)
{
if (array.Length == 0)
{
return;
}
var max = array.Max();
var min = array.Min();
var count = new int[max - min + 1];
var output = new int[array.Length];
for (var i = 0; i < array.Length; i++)
{
count[array[i] - min]++;
}
for (var i = 1; i < count.Length; i++)
{
count[i] += count[i - 1];
}
for (var i = array.Length - 1; i >= 0; i--)
{
output[count[array[i] - min] - 1] = array[i];
count[array[i] - min]--;
}
Array.Copy(output, array, array.Length);
}
}