Counting Sort is an integer sorting algorithm that operates by counting the occurrences of each unique element in the input list. It then uses this count information to place each element in its correct position in the output list. Counting Sort is particularly efficient when the range of the input data (the difference between the maximum and minimum values) is not significantly larger than the number of elements.
n is the number of elements in the input list, and k is the range of the input values.def countingSort(myList, length):
    maximum = myList[0]
    position = 0
    for i in range(1, length):
        if myList[i] > maximum:
            maximum = myList[i]
    tmpList = [0] * (maximum + 1)
    for i in range(0, length):
        tmpList[myList[i]] += 1
    for i in range(0, maximum + 1):
        for j in range(0, tmpList[i]):
            myList[position] = i
            position += 1
def printArray(myList, length):
    for i in range(0, length):
        print(myList[i], end=" ")
length = int(input("How many numbers do you want to sort? "))
myList = [] * length
i = 0
while i < length:
    value = int(input("Please enter the next number: "))
    myList.append(value)
    i += 1
print("\nArray to be sorted:")
printArray(myList, length)
print("\nGradual sorting:")
countingSort(myList, length)
print("Sorted array:")
printArray(myList, length)How many numbers do you want to sort? 6
Please enter the next number: 88
Please enter the next number: 23
Please enter the next number: 2
Please enter the next number: 5
Please enter the next number: 9
Please enter the next number: 12
Array to be sorted:
88 23 2 5 9 12 
Gradual sorting:
Sorted array:
2 5 9 12 23 88The function starts by determining the maximum value in the input list myList and stores it in the maximum variable. It then creates a temporary list tmpList of size maximum + 1 to store the count of each integer in the input list. This is done using the loop:
for i in range(0, length):
    tmpList[myList[i]] += 1The next step is to populate the input list myList with the sorted elements, which is done using the following loop:
for i in range(0, maximum + 1):
    for j in range(0, tmpList[i]):
        myList[position] = i
        position += 1In this loop, the elements from tmpList are added to myList in the sorted order. The function printArray is used to print the input list myList and the sorted list.
In the main part of the code, the user is asked to enter length numbers, which are stored in the list myList. The input list is then passed to the countingSort function for sorting, and the sorted list is printed using the printArray function.