Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up
Counting Sort for Limited Range

Counting Sort for Limited Range

Beginner 5 min read 23 Sep 2025 1.63K views
Counting Sort for Limited Range

Counting Sort for Limited Range

The Counting Sort Algorithm is a non-comparison-based sorting algorithm that is highly efficient when the range of input values is limited (i.e., the difference between the maximum and minimum values is small compared to the number of elements). It works by counting the occurrences of each value in the input array, then using these counts to place elements in their correct positions in the output array. Counting sort is stable and has a time complexity of O(n + k), where n is the number of elements and k is the range of input values.

Build a strong foundation for high-paying tech roles with DSA expertise. Enroll in our Free DSA Course today!

Example:

  • Input: nums = [4, 2, 2, 8, 3, 3, 1]
  • Output: [1, 2, 2, 3, 3, 4, 8]
  • Explanation: The array is sorted in ascending order, assuming the range of values is small (e.g., 0 to 8).

Logic

Enter an array of non-negative numbers (comma-separated) to sort it using the Counting Sort algorithm. Ensure the range of values is limited for best performance.

Program (Python - Counting Sort Implementation)

The following Python code implements the Counting Sort algorithm, optimized for arrays with a limited range of non-negative integers. The time complexity is O(n + k), where n is the number of elements and k is the range of values.


def countingSort(nums):
    # Find the range of the input
    max_val = max(nums)
    min_val = min(nums)
    range_val = max_val - min_val + 1
    
    # Initialize count array
    count = [0] * range_val
    output = [0] * len(nums)
    
    # Count occurrences of each value
    for num in nums:
        count[num - min_val] += 1
    
    # Compute cumulative count
    for i in range(1, range_val):
        count[i] += count[i - 1]
    
    # Build the output array
    for num in reversed(nums):
        output[count[num - min_val] - 1] = num
        count[num - min_val] -= 1
    
    return output

# Example usage
nums = [4, 2, 2, 8, 3, 3, 1]
print(countingSort(nums))  # Output: [1, 2, 2, 3, 3, 4, 8]

Output

For the input nums = [4, 2, 2, 8, 3, 3, 1], the output is:

[1, 2, 2, 3, 3, 4, 8]

Explanation

The array is sorted in ascending order using the counting sort algorithm, assuming a limited range of values (e.g., 1 to 8).

The tech industry projects 25% job growth for Java full stack developers by 2026. Lead the charge—join our Java Full Stack Course and excel!

Test yourself in 5 minutes

Take the free Datastructures skill challenge to see your strengths and the gaps to fix next.

Start the challenge
Share LinkedIn X Facebook WhatsApp

About the author

Ankur Mistry

Ankur Mistry

Author, Speaker and Coach

He has over 12 years of experience in liaising effectively between clients and different team members have been the hallmark. His technical prowess and capability of exploring new frontiers of technology & imparting them to his aspiring team members are his trademark.

He is graduated from South Gujarat University Gujarat-India, working as a Certified Scrum Master and Project Manager with a demonstrated history of working in the information technology and services industry. Skilled in C#, ASP.NET, SQL, ASP.NET MVC, Requirements Analysis, Agile Scrum, DevOps, and Software Project Management, Strong program and project management professional.

His execution is priceless & bringing forth his personal approach will help you realize your dreams, goals, and aspirations into reality.

Keep learning

Courses that go with this tutorial