The Second Three

Counting sort over keys 0…50 \dots 5 runs on

index:  0  1  2  3  4  5  6
A    = [3, 1, 4, 1, 5, 3, 1]

After tallying and prefix-summing, C[v] holds the number of elements with key at most v:

v 0 1 2 3 4 5
C[v] 0 3 3 5 6 7

The output loop is the stable one: for j from n-1 down to 0, place A[j] at B[C[A[j]] - 1] and then decrement C[A[j]]. Which index of B receives A[5], the second 3 in the input?

A

2

B

4

C

3

D

5