blob: 383e0039f62429ce3b3b78806a89ea70e0b77bf1 (
plain) (
blame)
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
|
#include <unistd.h> // size_t
void counting_sort(int* arr, size_t arrLength) {
constexpr int MAX_SIZE = 1e5;
int arr_copy[MAX_SIZE];
int max = arr[0];
for (int i = 0; i < arrLength; ++i) {
if (max < arr[i]) {
max = arr[i];
}
arr_copy[i] = arr[i];
}
int count[MAX_SIZE];
for (int i = 0; i < max + 1; ++i) {
count[i] = 0;
}
for (int i = 0; i < arrLength; ++i) {
count[arr[i]]++;
}
for (int i = 1; i <= max; ++i) {
count[i] += count[i - 1];
}
for (int i = arrLength - 1; i >= 0; --i) {
arr[count[arr_copy[i]] - 1] = arr_copy[i];
count[arr_copy[i]]--;
}
}
|