Python Program for Counting Sort (original) (raw)
Last Updated : 28 Jul, 2022
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
def
countSort(arr):
`` output
=
[
0
for
i
in
range
(
256
)]
`` count
=
[
0
for
i
in
range
(
256
)]
`` ans
=
[""
for
_
in
arr]
`` for
i
in
arr:
`` count[
ord
(i)]
+
=
1
`` for
i
in
range
(
256
):
`` count[i]
+
=
count[i
-
1
]
`` for
i
in
range
(
len
(arr)):
`` output[count[
ord
(arr[i])]
-
1
]
=
arr[i]
`` count[
ord
(arr[i])]
-
=
1
`` for
i
in
range
(
len
(arr)):
`` ans[i]
=
output[i]
`` return
ans
arr
=
"geeksforgeeks"
ans
=
countSort(arr)
print
(
"Sorted character array is %s"
%
("".join(ans)))
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!
Similar Reads
- Python Program for Bitonic Sort Bitonic Sequence: A sequence is called Bitonic if it is first increasing, then decreasing. In other words, an array arr[0..n-i] is Bitonic if there exists an index i where 0<=i<=n-1 such that x0 <= x1 …..<= xi and xi >= xi+1….. >= xn-1 A sequence, sorted in increasing order is cons 4 min read
- Python List Counting Programs Python provides various methods, such as count(), dictionary-based counting, and list comprehensions, to efficiently handle counting operations. This collection of Python programs covers different ways to count elements in lists, including counting occurrences, matching elements, frequency analysis, 2 min read
- 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 7 min read
- Python - Sort by Factor count Given element list, sort by factor count of each element. Input : test_list = [12, 100, 22] Output : [22, 12, 100] Explanation : 3, 5, 8 factors respectively of elements. Input : test_list = [6, 11] Output : [11, 6] Explanation : 1, 4 factors respectively of elements. Method #1 : Using sort() + len( 4 min read
- Python - Sort Matrix by Palindrome count Given a String Matrix of N characters, sort each row by the count of the palindromic string in it. Input : test_list = [["nitin", "meem", "geeks"], ["peep"], ["gfg", "is", "best"], ["sees", "level", "mom", "noon"]] Output : [['peep'], ['gfg', 'is', 'best'], ['nitin', 'meem', 'geeks'], ['sees', 'leve 8 min read
- Python program to count words in a sentence In this article, we will explore different methods for counting words in a sentence. The split() method is one of the simplest and most efficient ways to count words in a sentence. [GFGTABS] Python s = "Python is fun and versatile." # Counting words word_count = len(s.split()) print(word_c 2 min read
- Python Program to Count Non-Bouncy numbers If any number is represented in such a way that when we are reading it from left to right each ith Digit is greater or equal than i-1th digit is known as an increasing number. And if digits of any number are decreasing from left to right it's known as decreasing number.` Example: Increasing Number ? 2 min read
- Python List and Tuple Combination Programs Lists and tuples are two of the most commonly used data structures in Python. While lists are mutable and allow modifications, tuples are immutable and provide a stable structure for storing data. This article explores various programs related to list and tuple combinations, covering topics like: So 6 min read
- Python program to count the number of spaces in string In Python, there are various ways to Count the number of spaces in a String. Using count() Methodcount() method in Python is used to return the number of occurrences of a specified element in a list or string [GFGTABS] Python s = "Count the spaces in this string." # Count spaces using the 3 min read
- Python Program to Count 1's in a sorted binary array Given a binary array sorted in non-increasing order, count the number of 1's in it. Examples: Input: arr[] = {1, 1, 0, 0, 0, 0, 0} Output: 2 Input: arr[] = {1, 1, 1, 1, 1, 1, 1} Output: 7 Input: arr[] = {0, 0, 0, 0, 0, 0, 0} Output: 0 A simple solution is to linearly traverse the array. The time c 2 min read