forked from r0acho/CountingSort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
60 lines (46 loc) · 1.25 KB
/
Program.cs
File metadata and controls
60 lines (46 loc) · 1.25 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
//сортировка подсчетом
int[] array = {-10, -5, -9, 0, 2, 5, 1, 3, 1, 0, 1};
int[] sortedArray = CountingSortExtended(array);
//CountingSort(array);
Console.WriteLine(string.Join(", ", sortedArray));
void CountingSort(int[] inputArray)
{
int[] counters = new int[10]; //массив повторений
for (int i = 0; i < inputArray.Length; i++)
{
counters[inputArray[i]]++;
// ourNumber = inputArray[i];
// counters[ourNumber]++;
}
int index = 0;
for (int i = 0; i < counters.Length; i++)
{
for (int j = 0; j < counters[i]; j++)
{
inputArray[index] = i;
index++;
}
}
}
int[] CountingSortExtended(int[] inputArray)
{
int max = inputArray.Max();
int min = inputArray.Min();
int offset = -min;
int[] sortedArray = new int[inputArray.Length];
int[] counters = new int[max + offset + 1];
for (int i = 0; i < inputArray.Length; i++)
{
counters[inputArray[i] + offset]++;
}
int index = 0;
for (int i = 0; i < counters.Length; i++)
{
for (int j = 0; j < counters[i]; j++)
{
sortedArray[index] = i - offset;
index++;
}
}
return sortedArray;
}