Counting Sort Algorithm

Video Tutorial
FREE
Counting Sort thumbnail
This video belongs to
DSA Problem Solving for Interviews using Java
11 modules
Certificate
Topics Covered

Counting Sort Algorithm is a sorting algorithm that works best when you have a small range of numbers to sort. Instead of comparing elements like in other sorting methods, it counts how many times each number appears and then puts them in order based on that count. This makes it really fast when the range of numbers is not too big compared to the total number of items to be sorted.

Working of counting Sort Algorithm

  1. Determine the maximum element, denoted as "max," from the provided array.

given array

  1. Create an array of length "max+1" with all elements initialized to 0. This array serves the purpose of storing the count of each element present in the given array.

count array

  1. Assign the count of each element to their corresponding index in the count array.

count array 1.webp

  1. Calculate the cumulative sum of the elements in the count array. This cumulative sum assists in correctly positioning the elements in the sorted array.

count array 2

  1. Determine the index of each element from the original array in the count array, yielding the cumulative count. Place each element at the calculated index position in the sorted array, as illustrated in the figure below.

sorted array.webp

  1. Once each element is correctly positioned, decrement its count by one in the count array.

Implementation of counting sort Algorithm

Counting sort in Java

Output:

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Counting sort in C/C++

Output:

Counting sort in Python

Output:

Complexity Analysis of Counting Sort Algorithm

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Time Complexity of Counting Sort Algorithm

The time complexity of the algorithm is O(N+M), where N denotes the size of the input array and M is the size of the count array. This holds true for worst-case, average-case, and best-case scenarios.

Space Complexity of Counting Sort Algorithm

The Space Complexity is O(N+M), accounting for the space used by the output array and the count array.

Applications of Counting Sort Algorithm

  1. Counting sort Algorithm is employed for sorting arrays containing smaller integers with numerous repetitions.
  2. It's chosen when achieving linear time complexity is crucial for efficient sorting operations.
  3. Its effectiveness lies in efficiently handling scenarios where elements have limited range and frequent repetitions.

Advantage of Counting Sort Algorithm

  1. Counting sort outperforms comparison-based sorting algorithms, like merge sort and quicksort, for inputs with a range comparable to the input size.
  2. It offers simplicity in implementation, making it straightforward to code.
  3. Counting sort maintains stability, preserving the original order of elements with equal values in the sorted output.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Disadvantage of Counting Sort Algorithm

  1. Counting sort is incompatible with decimal values, limiting its applicability in sorting operations involving non-integer data.
  2. Its efficiency diminishes significantly when sorting a large range of values, rendering it inefficient for such scenarios.
  3. As a non-in-place sorting algorithm, counting sort necessitates additional space to store auxiliary data, impacting its memory usage compared to in-place algorithms.

Conclusion

  1. Counting Sort Algorithm provides an efficient means of sorting arrays with small integer values and numerous repetitions.
  2. Its linear time complexity makes it ideal for scenarios where achieving fast sorting operations is crucial.
  3. Counting Sort maintains stability, preserving the original order of elements with equal values.
  4. Despite its effectiveness, Counting Sort is limited by its inability to handle decimal values and its decreased efficiency with large range inputs.
  5. Nevertheless, its simplicity in implementation and ability to handle repetitive elements efficiently contribute to its practical significance.
  6. Careful consideration of the input data characteristics is essential to determine Counting Sort's suitability for specific sorting tasks.

Thanks for reading. May the code be with you 🙂

Code with Precision! Enroll in Scaler Academy's Online DSA Certification Course and Master Efficient Algorithms.

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more