| 1 | Find max value | [4, 2, 2, 8, 3, 3, 1] | N/A | N/A | max = 8 | Input array unchanged |
| 2 | Initialize count array with zeros | [4, 2, 2, 8, 3, 3, 1] | [0, 0, 0, 0, 0, 0, 0, 0, 0] | N/A | count array size = 9 | Count array all zeros |
| 3 | Count occurrences | [4, 2, 2, 8, 3, 3, 1] | [0, 1, 2, 2, 1, 0, 0, 0, 1] | N/A | count[1]=1, count[2]=2, count[3]=2, count[4]=1, count[8]=1 | Count array updated with frequencies |
| 4 | Cumulative count | [4, 2, 2, 8, 3, 3, 1] | [0, 1, 3, 5, 6, 6, 6, 6, 7] | N/A | count[i] = count[i] + count[i-1] for i=1 to 8 | Count array stores positions |
| 5 | Build output array (process from right to left) | [4, 2, 2, 8, 3, 3, 1] | [0, 1, 3, 5, 6, 6, 6, 6, 7] | [1, 2, 2, 3, 3, 4, 8] | Place 1 at output[count[1]-1=0], decrement count[1]=0
Place 3 at output[count[3]-1=4], decrement count[3]=4
Place 3 at output[count[3]-1=3], decrement count[3]=3
Place 8 at output[count[8]-1=6], decrement count[8]=6
Place 2 at output[count[2]-1=2], decrement count[2]=2
Place 2 at output[count[2]-1=1], decrement count[2]=1
Place 4 at output[count[4]-1=5], decrement count[4]=5 | Output array built in sorted order |
| 6 | Copy output to input array | [1, 2, 2, 3, 3, 4, 8] | [0, 0, 1, 3, 5, 6, 6, 6, 6] | [1, 2, 2, 3, 3, 4, 8] | arr[i] = output[i] for i=0 to 6 | Input array now sorted |
| 7 | End | [1, 2, 2, 3, 3, 4, 8] | N/A | N/A | Sorting complete | Final sorted array |