we calculate each digit of the number, after which we count the cumulative sum, why is it necessary?
# count of each digit in arr
for i in range(len(arr)):
index = (arr[i] // place) % 10
count[index] += 1
# calculate cumulative count
for i in range(1, 10):
count[i] += count[i - 1]
This is done to set the ending indexes of the destination array's logical bins during a radix sort pass.
Say there are 7 zeroes, 5 ones, 3 twos, 4 threes, ..., then the result is:
Using the questions code:
During a radix sort pass, the array is scanned and stored backwards.
This can be changed to set starting indexes and scanning the array in forwards order:
Then the result is:
During a radix sort pass, the array is scanned and stored forwards
Example 4 pass base 256 radix sort for positive 32 bit integers.