Counting Sort - Javatpoint
Counting Sort - Javatpoint
Home Python Java JavaScript HTML SQL PHP C#
DS Tutorial
DS Tutorial
DS Introduction
DS Algorithm
Asymptotic Analysis
DS Pointer
DS Structure
DS Array
DS Array
2D Array
DS Linked List
Linked List
https://www.javatpoint.com/counting-sort 1/24
1/20/25, 9:07 PM Counting Sort - javatpoint
Skip list in DS
DS Stack
DS Stack
Array Implementation
← prev next →
This sorting technique doesn't perform sorting by comparing elements. It performs sorting
by counting objects having distinct key values like hashing. After that, it performs some
arithmetic operations to calculate each object's index position in the output sequence.
Counting sort is not used as a general-purpose sorting algorithm.
Counting sort is effective when range is not greater than number of objects to be sorted. It
can be used to sort the negative input values.
Algorithm
for i = n to 1
Restore the array elements
Decrease the count of every restored element by 1
end countingSort
To understand the working of the counting sort algorithm, let's take an unsorted array. It
will be easier to understand the counting sort via an example.
1. Find the maximum element from the given array. Let max be the maximum element.
2. Now, initialize array of length max + 1 having all 0 elements. This array will be used to
store the count of the elements in the given array.
3. Now, we have to store the count of each array element at their corresponding index in the
count array.
The count of an element will be stored as - Suppose array element '4' is appeared two times,
so the count of element 4 is 2. Hence, 2 is stored at the 4th position of the count array. If any
element is not present in the array, place 0, i.e. suppose element '3' is not present in the
array, so, 0 will be stored at 3rd position.
https://www.javatpoint.com/counting-sort 3/24
1/20/25, 9:07 PM Counting Sort - javatpoint
Now, store the cumulative sum of count array elements. It will help to place the elements at
the correct index of the sorted array.
After placing element at its place, decrease its count by one. Before placing element 2, its
count was 2, but after placing it at its correct position, the new count for element 2 is 1.
https://www.javatpoint.com/counting-sort 4/24
1/20/25, 9:07 PM Counting Sort - javatpoint
1. Time Complexity
Best Case Complexity - It occurs when there is no sorting required, i.e. the array
is already sorted. The best-case time complexity of counting sort is O(n + k).
Average Case Complexity - It occurs when the array elements are in jumbled
order that is not properly ascending and not properly descending. The average
case time complexity of counting sort is O(n + k).
https://www.javatpoint.com/counting-sort 5/24
1/20/25, 9:07 PM Counting Sort - javatpoint
Worst Case Complexity - It occurs when the array elements are required to be
sorted in reverse order. That means suppose you have to sort the array elements
in ascending order, but its elements are in descending order. The worst-case
time complexity of counting sort is O(n + k).
In all above cases, the time complexity of counting sort is same. This is because the
algorithm goes through n+k times, regardless of how the elements are placed in the array.
Counting sort is better than the comparison-based sorting techniques because there is no
comparison between elements in counting sort. But, when the integers are very large the
counting sort is bad because arrays of that size have to be created.
2. Space Complexity
Stable YES
The space complexity of counting sort is O(max). The larger the range of
elements, the larger the space complexity.
#include<stdio.h>
https://www.javatpoint.com/counting-sort 6/24
1/20/25, 9:07 PM Counting Sort - javatpoint
/* This loop will find the index of each element of the original array in count array, a
nd
place the elements in output array*/
for (int i = n - 1; i >= 0; i--) {
output[count[a[i]] - 1] = a[i];
count[a[i]]--; // decrease count for same numbers
}
https://www.javatpoint.com/counting-sort 7/24
1/20/25, 9:07 PM Counting Sort - javatpoint
int main() {
int a[] = { 11, 30, 24, 7, 31, 16 };
int n = sizeof(a)/sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
countSort(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
Output
#include <iostream>
{
int output[n+1];
int max = getMax(a, n);
int count[max+1]; //create count array with size [max+1]
/* This loop will find the index of each element of the original array in count array, a
nd
place the elements in output array*/
for (int i = n - 1; i >= 0; i--) {
output[count[a[i]] - 1] = a[i];
count[a[i]]--; // decrease count for same numbers
}
https://www.javatpoint.com/counting-sort 9/24
1/20/25, 9:07 PM Counting Sort - javatpoint
int main() {
int a[] = { 31, 11, 42, 7, 30, 11 };
int n = sizeof(a)/sizeof(a[0]);
cout<<"Before sorting array elements are - \n";
printArr(a, n);
countSort(a, n);
cout<<"\nAfter sorting array elements are - \n";
printArr(a, n);
return 0;
}
Output
using System;
class CountingSort {
https://www.javatpoint.com/counting-sort 10/24
1/20/25, 9:07 PM Counting Sort - javatpoint
/* This loop will find the index of each element of the original array in count array, a
nd
place the elements in output array*/
for (int i = n - 1; i >= 0; i--) {
output[count[a[i]] - 1] = a[i];
count[a[i]]--; // decrease count for same numbers
}
https://www.javatpoint.com/counting-sort 11/24
1/20/25, 9:07 PM Counting Sort - javatpoint
Output
class CountingSort {
{
count[i] = 0; // Initialize count array with all zeros
}
/* This loop will find the index of each element of the original array in
https://www.javatpoint.com/counting-sort 13/24
1/20/25, 9:07 PM Counting Sort - javatpoint
int n = a.length;
CountingSort c1 = new CountingSort();
System.out.println("\nBefore sorting array elements are - ");
c1.printArray(a, n);
c1.countSort(a,n);
System.out.println("\nAfter sorting array elements are - ");
c1.printArray(a, n);
System.out.println();
}
}
Output
<?php
https://www.javatpoint.com/counting-sort 14/24
1/20/25, 9:07 PM Counting Sort - javatpoint
for ($i = 0; $i < $n; $i++) // Store the count of each element
{
$count[$a[$i]]++;
}
/* This loop will find the index of each element of the original array in
count array, and
place the elements in output array*/
for ($i = $n - 1; $i >= 0; $i--) {
$output[$count[$a[$i]] - 1] = $a[$i];
$count[$a[$i]]--; // decrease count for same numbers
}
https://www.javatpoint.com/counting-sort 15/24
1/20/25, 9:07 PM Counting Sort - javatpoint
$n = count($a);
echo "Before sorting array elements are - <br>";
printArray($a, $n);
countSort($a,$n);
echo "<br> After sorting array elements are - <br>";
printArray($a, $n);
?>
Output
So, that's all about the article. Hope the article will be helpful and informative to you.
This article was not only limited to the algorithm. We have also discussed counting sort
complexity, working, and implementation in different programming languages.
← prev next →
Related Posts
Selection Sort
Algorithm In this article, we will discuss the Selection sort Algorithm. The working
procedure of selection sort is also simple. This article will be very helpful and interesting to
students as they might face selection sort as a question in their examinations. So, it is
important...
https://www.javatpoint.com/counting-sort 16/24
1/20/25, 9:07 PM Counting Sort - javatpoint
8 min read
Heap Sort
Algorithm In this article, we will discuss the Heapsort Algorithm. Heap sort processes the
elements by creating the min-heap or max-heap using the elements of the given array.
Min-heap or max-heap represents the ordering of array in which the root element
represents the minimum or maximum...
10 min read
Shell Sort
Algorithm In this article, we will discuss the shell sort algorithm. Shell sort is the
generalization of insertion sort, which overcomes the drawbacks of insertion sort by
comparing elements separated by a gap of several positions. It is a sorting algorithm that is
an extended version of...
7 min read
Merge Sort
Algorithm In this article, we will discuss the merge sort Algorithm. Merge sort is the sorting
technique that follows the divide and conquer approach. This article will be very helpful
and interesting to students as they might face merge sort as a question in their
examinations....
17 min read
Cycle Sort
Cycle sort algorithm In this article, we will discuss the Cycle sort Algorithm. Cycle sort is a
comparison sorting algorithm that forces array to be factored into the number of cycles
where each of them can be rotated to produce a sorted array. It is theoretically optimal...
16 min read
https://www.javatpoint.com/counting-sort 17/24
1/20/25, 9:07 PM Counting Sort - javatpoint
Cocktail Sort
Algorithm In this article, we will discuss the cocktail sort algorithm. Cocktail sort is the
variation of Bubble Sort, which traverses the list in both directions alternatively. It is
different from bubble sort in the sense that bubble sort traverses the list in the forward
direction...
10 min read
Comb Sort
Algorithm In this article, we will discuss the comb sort Algorithm. is the advanced form of
bubble Sort. Bubble Sort compares all the adjacent values while comb sort removes all the
turtle values or small values near the end of the list. It is a comparison-based...
9 min read
Tim Sort
Algorithm In this article, we will discuss the Tim sort Algorithm. Tim-sort is a sorting
algorithm derived from insertion sort and merge sort. It was designed to perform optimally
on different kind of real-world data. Tim sort is an adaptive sorting algorithm that needs
O(n log n)...
15 min read
Bucket Sort
Algorithm In this article, we will discuss the bucket sort Algorithm. The data items in the
bucket sort are distributed in the form of buckets. In coding or technical interviews for
software engineers, sorting algorithms are widely asked. So, it is important to discuss the
topic. Bucket...
9 min read
Sorting Algorithms
https://www.javatpoint.com/counting-sort 18/24
1/20/25, 9:07 PM Counting Sort - javatpoint
Sorting is the process of arranging the elements of an array so that they can be placed
either in ascending or descending order. For example, consider an array A = {A1, A2, A3, A4,
?? An }, the array is called to be in ascending...
3 min read
Python Java
Javascript HTML
Database PHP
C++ React
https://www.javatpoint.com/counting-sort 19/24
1/20/25, 9:07 PM Counting Sort - javatpoint
B.Tech / MCA
Data
DBMS
Structures
Operating
DAA
System
Computer Compiler
Network Design
Computer Discrete
Organization Mathematics
Ethical Computer
Hacking Graphics
https://www.javatpoint.com/counting-sort 20/24
1/20/25, 9:07 PM Counting Sort - javatpoint
Web Software
Technology Engineering
Cyber
Automata
Security
C
C++
Programming
Java .Net
Python Programs
https://www.javatpoint.com/counting-sort 21/24
1/20/25, 9:07 PM Counting Sort - javatpoint
Control Data
System Warehouse
Preparation
Aptitude Reasoning
Verbal Interview
Ability Questions
Company
Questions
https://www.javatpoint.com/counting-sort 22/24
1/20/25, 9:07 PM Counting Sort - javatpoint
https://www.javatpoint.com/counting-sort 23/24
1/20/25, 9:07 PM Counting Sort - javatpoint
https://www.javatpoint.com/counting-sort 24/24