for(int j = size-1; j >= 0; j--) {
B[--C[A[j]]] = A[j];
}
is equivalent to:
for(int j = size-1; j >= 0; j--) {
int element = A[j];
int pos = C[element] - 1;
B[pos] = element;
C[element]--;
}
Imagine array 1 0 1
. Now counts of elements would be following:
0
- 1 time
1
- 2 times
The preparation of positions increments counts by the amount of elements that precede them:
0
- 1
1
- 3
Position of elements in new (sorted) array is now (count - 1):
position of 0
= 1 - 1 = 0
position of first 1
= 3 - 1 = 2
position of second 1
= 2 - 1 = 1
making it 0 1 1
.