Python Program for Counting Sort Last Updated : 28 Jul, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence. Python3 # Python program for counting sort # The main function that sort the given string arr[] in # alphabetical order def countSort(arr): # The output character array that will have sorted arr output = [0 for i in range(256)] # Create a count array to store count of individual # characters and initialize count array as 0 count = [0 for i in range(256)] # For storing the resulting answer since the # string is immutable ans = ["" for _ in arr] # Store count of each character for i in arr: count[ord(i)] += 1 # Change count[i] so that count[i] now contains actual # position of this character in output array for i in range(256): count[i] += count[i-1] # Build the output character array for i in range(len(arr)): output[count[ord(arr[i])]-1] = arr[i] count[ord(arr[i])] -= 1 # Copy the output array to arr, so that arr now # contains sorted characters for i in range(len(arr)): ans[i] = output[i] return ans # Driver program to test above function arr = "geeksforgeeks" ans = countSort(arr) print ("Sorted character array is %s" %("".join(ans))) # This code is contributed by Nikhil Kumar Singh Output: Sorted character array is eeeefggkkorss Time Complexity: O(n+k) where n is the number of elements in the input array and k is the range of input. Auxiliary Space: O(n+k) Please refer complete article on Counting Sort for more details! Comment More infoAdvertise with us Next Article Python sorted() Function K kartik Follow Improve Article Tags : Python Practice Tags : python Similar Reads Counting Sort - Python Counting Sort is a non-comparison-based sorting algorithm. It is particularly efficient when the range of input values is small compared to the number of elements to be sorted. The basic idea behind Counting Sort is to count the frequency of each distinct element in the input array and use that info 6 min read Output of python program | Set 2 Difficulty level : IntermediatePredict the output of following Python Programs. Program 1: Python3 class Acc: def __init__(self, id): self.id = id id = 555 acc = Acc(111) print (acc.id) Output: 111 Explanation: Instantiation of the class "Acc" automatically calls the method __init__ and passes the o 2 min read Python sorted() Function sorted() function returns a new sorted list from the elements of any iterable like (e.g., list, tuples, strings ). It creates and returns a new sorted list and leaves the original iterable unchanged. Let's start with a basic example of sorting a list of numbers using the sorted() function.Pythona = 3 min read sort() in Python sort() method in Python sort the elements of a list in ascending or descending order. It modifies the original list in place, meaning it does not return a new list, but instead changes the list it is called on. Example:Pythona = [5, 3, 8, 1, 2] a.sort() print(a) a.sort(reverse=True) print(a)Output[1 4 min read Python sorted containers | An Introduction Sorted Containers is a powerful Python library that provides fast and easy-to-use implementations of SortedList, SortedDict and SortedSet data types. Unlike Pythonâs built-in types, these containers maintain their elements in sorted order automatically as elements are added or removed. To use this l 3 min read numpy.count() in Python numpy.core.defchararray.count(arr, substring, start=0, end=None): Counts for the non-overlapping occurrence of sub-string in the specified range. Parameters: arr : array-like or string to be searched. substring : substring to search for. start, end : [int, optional] Range to search in. Returns : An 1 min read Like