TEC-Bridge Logo

Counting Sort Visualizer

STEM Interactive Visual Learning Program at TEC-Bridge AI

Setup Array

Sort Controls

Array Visualization

Original Array:
Count Array:
Output Array:

Algorithm Steps

How to Use

  1. Setup: Enter numbers 0-9 separated by commas or click "Random" to generate an array
  2. Sort: Click "Start Sort" to begin the counting sort process
  3. Step Through: Click "Next Step" to see each step of the algorithm execution
  4. Run Through: Click "Run Through" to automatically execute all steps in sequence
  5. Observe: Watch the counting and placement process
  6. Reset: Click "Reset" to start over with a new sort

Colors: Orange = Counting, Blue = Current, Purple = Placing, Yellow = Count Array, Green = Sorted

Counting Sort Concept

Counting Sort is a non-comparison sorting algorithm that sorts elements by counting occurrences of each distinct element.

How it works:

  • Create count array for range of input values
  • Count occurrences of each element
  • Transform counts to actual positions
  • Place elements in output array using counts
  • Works only for limited range of integers

Time Complexity: O(n + k) where k is range of input

Use Cases

  • Limited Range Integers: Optimal for sorting integers within a known, small range (0 to k)
  • Radix Sort Foundation: Core algorithm used in radix sort for multi-digit number sorting
  • Grade/Score Sorting: Sorting grades (0-100), ratings (1-5), or bounded scores efficiently
  • Frequency Analysis: Naturally counts occurrences while sorting, perfect for frequency tables
  • Stable Sorting Needed: Maintains relative order of equal elements unlike quick sort
  • Linear Time Critical: O(n + k) performance essential for large datasets with small ranges
  • Histogram Generation: Count array directly provides frequency distribution histogram
  • Educational Algorithm Study: Perfect for learning non-comparison sorting paradigms

Real-World Example

Scenario: A school needs to sort 500 student test scores (range 0-100) and generate a frequency distribution to analyze class performance across score brackets.

Input: 500 test scores between 0-100 (e.g., [78, 92, 65, 88, 95, 72, 78, 85, 88, 92...])
Process: Create count array for each score 0-100, count occurrences, output sorted scores maintaining stability
Output: Sorted scores [65, 68, 72, 74, 77, 78, 78, 81, 85, 87, 88, 88, 89, 90, 91, 92, 92, 95...] with frequency histogram

Counting sort excels here because test scores are bounded integers (0-100), and processing is O(n + k) = O(500 + 100) versus comparison sort's O(n log n) = O(500 * log 500). The count array simultaneously provides the sorted output and a frequency histogram showing how many students scored in each range. This is stable sorting, so students with identical scores maintain their original order, and the algorithm works directly in linear time without recursion overhead.

Benefits: Linear time O(n+k), stable sort, frequency distribution included, ideal for bounded integer ranges

Counting Sort Code Implementation

Rate This Learning Tool

Clarity of Visualization:
★ ★ ★ ★ ★
Interactivity:
★ ★ ★ ★ ★
Educational Value:
★ ★ ★ ★ ★
Performance:
★ ★ ★ ★ ★
Intuitivity & Usability:
★ ★ ★ ★ ★
Overall Evaluation:
★ ★ ★ ★ ★

© 2025 TEC-Bridge AI. All rights reserved. | stemists.com@gmail.com | https://stemists.com