Hi, in this tutorial, we are going to write a simple program implementation for Counting Sort Algorithm in Python.

## Counting Sort Algorithm

Before writing the source code or program for count sort, we first understand **what is Count Sort Algorithm** in programming.

So, Counting Sort is an Integer-based algorithm that means the numbers or the values of the input received from the user must be or assumed to be integers like in bucket sort and radix sort algorithm.

One thing about Count Sort is that it is among the fastest sorting algorithms around in data structures.

The particular distinction for count sort is that it creates a bucket for each value and keep a counter in each bucket.

Then each time a value is encountered in the input collection, the appropriate counter is incremented.

Because count sort creates a bucket for each value, an imposing restriction is that the maximum value in the input array is known beforehand.

Counting sort is used when there are smaller integers with multiple or large counts in a particular array.

### Implementation of Count Sort

#### 1. Define Counting Sort Function

So first we are going to define a Counting Sort function which basically accepts the input array from the user and then returns sorted array back to the function call.

Now, what we are going to do first is to find the maximum value of an integer in an array and then assign buckets according to the maximum value of the array.

Since the values range from 0 to k, create k+1 buckets.

Now, we have to fill those buckets, so for that iterate through the input array and every time when an item appears, increment the counter in its bucket.

After this iterate through the buckets and we know that each bucket represent value in array

So for each bucket, from smallest value to largest, add the index of the bucket to the input array and decrease the counter in said bucket by one; until the counter is zero.

**Worst Case Complexity: O(n+k)Best Case Complexity: O(n+k)Average Case Complexity: O(n+k)**

where n is the size of the input array and k means the values range from 0 to k.

#### 2. Define Main Function

Since we have implemented counting sort in the first step, now let’s call the counting sort function from step 1 in the main method.

It will simply accept list input by the user and then return the sorted list back to the function call and then we will print the sorted list back to the console.

#### Source Code

```
def countingSort(myList):
maxValue = 0
for i in range(len(myList)):
if myList[i] > maxValue:
maxValue = myList[i]
buckets = [0] * (maxValue + 1)
for i in myList:
buckets[i] += 1
i = 0
for j in range(maxValue + 1):
for a in range(buckets[j]):
myList[i] = j
i += 1
return myList
if __name__ == '__main__':
sortedList = countingSort([1,23,4,5,6,7,8])
print(sortedList)
```

##### Output

Hope you guys like the tutorial, feel free to drop any comments in the comment section down below.

Pingback: 20+ Python Exercises with Input and Output - Part 1 | Codez Up