My implementation of counting sort I've made the following implementation of counting sort. I don't think that I understand the algorithm fully but this implementation seems to be working correctly. 

I've used the Counterdictionary from the collections module. It seems to be the best way to handle the counting part. But I haven't seen anyone using it to implement counting sort.

Is there any way to improve the code?

from random import randrange
from collections import Counter
from copy import deepcopy

def counting_sort(aList, length):
    """
    counting sort does not work for negative numbers
    counting sort assumes that each element is SMALL INTEGER

    running time is O(maxVal - minVal) ---&gt; Linear

    (Useful only when the difference between maxVal and minVal is not large)
    """
    list_copy = deepcopy(aList)  # a copy so that I can return it and check it against the original list applied to sorted function
    c = Counter()

    for num in list_copy:
        c[num] += 1

    index = 0

    # for i in range(len(c)):
        # while 0 &lt; c[i]:
            # list_copy[index] = i
            # c[i] -= 1
            # index += 1
    for key in c:
        while c[key] &gt; 0:
            list_copy[index] = key
            c[key] -= 1
            index += 1

    return list_copy


aList = [randrange(100) for _ in range(1000)]
after = counting_sort(aList, len(aList))
print(after == sorted(aList))
Your code is not reliable because Counter is unordered.  In many cases, it comes up with the right result, but do not depend on it.  I will not give an alternate solution because Code Review is for improvements on existing code, not for writing new code.  Although your code should not be used for sorting, I have a couple comments on what you did.



You aren't using length, so why require it for an argument?

Instead of creating a copy of a list and then changing each index, create an empty list and add things to it.

Your implementation of Counter does not take advantage of it.  You use it very similarly to how you would use a dictionary.  Just use Counter(aList)

You can simplify your for key in c: loop by using itertools.repeat.  Note: don't use it for mutable objects.

Your naming is inconsistent.  PEP 8 recommends snake_case, but you use both snake_case and pascalCase.  You aren't required to comply with PEP 8's recommendation, but you should at least be consistent.  You should also use more descriptive names than c, for example.

The new code:

from collections import Counter
from itertools import repeat

def counting_sort(number_list):
    """
    counting sort does not work for negative numbers
    counting sort assumes that each element is SMALL INTEGER

    running time is O(maxVal - minVal) ---&gt; Linear

    (Useful only when the difference between maxVal and minVal is not large)
    """

    sorted_list = []
    for value, amount in Counter(number_list).items():
        sorted_list.extend(repeat(value, amount))

    return sorted_list