COMPUTER SCIENCE · School · College

Non-Comparison Sorting Visualizer

How can numbers be sorted without comparing every pair? Use the values themselves as addresses, read one digit at a time or distribute records into ranges. Explore Counting, Radix and a hybrid Bucket sort to see what extra knowledge about the keys makes possible.

↓ Jump to simulatorFree · No loginReal-time controlsUnits & models explained
LIVE EXPERIMENTReady · Press Start
Your browser does not support canvas. Read the scientific lesson below.
Press Start to explore the model

Implementation scope: Ascending sort of 8–64 nonnegative integer keys restricted to 0–99. Counting uses k = 100 counters. LSD Radix always uses two stable decimal passes, b = 10 and d = 2. Bucket uses ten fixed-width ranges and stable Insertion inside each bucket. This is not a general sorter for negative numbers, floating-point values, strings or arbitrary user-defined objects. Instrumented event playback is not a wall-clock benchmark.

Non-Comparison Sorting Visualizer — static example view
Example view of this experiment. Use the simulator above to explore your selected parameters and live measurements.
LEARNING OUTCOMES

What will you understand?

  • Explain which assumptions let a sorting method go beyond the comparison model.
  • Trace frequency counts, prefix positions and stable output placement.
  • Explain why a least-significant-digit pass must be stable.
  • Recognize the comparison sorting inside a bucket-based method.
THE ALGORITHMS EXPLAINED

How the simulation works

Additional information changes the problem

A comparison-only sorter asks which of two keys comes first. Here a key is also an integer that can identify a counter or a digit. The lab accepts keys from 0 through 99, making a 100-counter array and two decimal digit passes sufficient. This restriction is essential: a method using one counter per possible key becomes impractical when the range is enormous compared with the number of records. The familiar comparison lower bound does not forbid linear-time sorting under suitable key assumptions.

Counting records is different from comparing keys

Counting and Radix have zero pairwise key comparisons in these implementations. They still read records, update counters, compute positions and write output. The main-write counter tracks writes to the displayed array; the auxiliary-write counter tracks updates to counts, output buffers and buckets. Allocation, zero initialization, scalar assignments, digit arithmetic and loop tests are excluded. Event totals therefore describe this teaching trace, not total CPU instructions or measured speed.

A stable pass preserves useful earlier work

After a Radix ones-digit pass, keys with the same ones digit preserve their incoming order. The next stable tens-digit pass groups keys by tens while retaining the ones order within each group. That is why composing the passes yields numerical order. The intermediate main array after only the ones pass is usually not numerically sorted; this is an expected stage, not an error. The record readout includes original positions to make equality and stability inspectable.

Bucket sort belongs to both conversations

The first stage assigns keys to value ranges without comparing pairs. Within each bucket, this lab uses comparison-based Insertion sort, then concatenates buckets in range order. Bucket is therefore presented here as a distribution method with a comparison-based inner step. Stable distribution plus stable insertion preserves equal-key order. Its comparison count can be nonzero, unlike Counting and LSD Radix.

ALGORITHM FIELD GUIDE

Choose a strategy, then trace its work

Properties of the specific implementations in this lab
AlgorithmWork boundExtra spaceStable?
Counting sortΘ(n + k), with k = 100 hereO(n + k)Yes, using stable output placement
LSD Radix sortΘ(d(n + b)), with d = 2 and b = 10 hereO(n + b)Yes, each digit pass is stable
Bucket sort (distribution + insertion)O(n + B + Σmᵢ²) upper bound; O(n² + B) worst caseO(n + B), with B = 10 hereYes for this stable inner-sort variant

Space bounds describe the sorting algorithm, excluding this visualizer's display and instrumentation. In the formulas, n is the number of records; k is the key range; b is the radix base; d is the number of digit passes; B is the bucket count and mᵢ is the size of bucket i.

Counting sort

Count how often each key occurs, then accumulate those counts into ending positions. Visit the input from right to left; decrement the matching ending position and place the whole record there in a separate output buffer. Copy that output back to the main array. Processing ties from right to left into successively earlier positions preserves their original order. Reconstructing only bare key values would hide record identities, so the lab keeps the records intact.

Small example: For [2a, 0, 2b, 1], frequencies are [1, 1, 2] over keys 0–2, giving cumulative totals [1, 2, 4]. Stable placement gives [0, 1, 2a, 2b]. The main array is updated after the output buffer is complete.

count each key
accumulate prefix totals
for records from right to left:
  decrement that key's ending position
  place record in the output buffer
copy output back to the main array

Watch for: Many prefix events can happen without any bar moving. Read the count operation instead of expecting each event to swap bars.

Try Counting sort in the simulator ↑

LSD Radix sort

Use a stable counting pass for the least significant digit, then repeat for more significant digits. Our decimal keys require ones and tens passes. Each pass uses ten counters and an output buffer. Leading tens digits of zero are handled naturally. Two decimal passes are chosen for teaching clarity; a machine implementation may use a different base and representation. The number of digits and the chosen base are part of the cost, not free constants in every problem.

Small example: For [21, 13, 12, 11], the ones pass gives [21, 11, 12, 13]. A stable tens pass then gives [11, 12, 13, 21]. Losing the order among the teen values during the second pass would lose the work done by the first.

for digit place in [ones, tens]:
  count that digit for every record
  accumulate ten prefix totals
  place records stably in an output buffer
  copy the buffer to the main array

Watch for: After the ones pass, compare only the ones digits. The full numerical ordering becomes meaningful after the final pass.

Try LSD Radix sort in the simulator ↑

Bucket sort (distribution + insertion)

Distribute keys into ten ranges: 0–9, 10–19 and so on. Sort each range with stable Insertion, then concatenate. If bucket i contains mᵢ records, insertion work depends on that bucket's size and order. The common expected-linear claim needs assumptions such as an appropriate distribution and a number of buckets that scales with n. This lab keeps ten buckets, so it does not claim linear asymptotic growth for arbitrarily large inputs. All records concentrated in one badly ordered bucket produce quadratic inner-sort work.

Small example: The keys [42, 17, 48, 12] distribute into bucket 1 containing [17, 12] and bucket 4 containing [42, 48]. Sorting those buckets and concatenating produces [12, 17, 42, 48]. Comparisons occur inside the buckets, not during range assignment.

append each record to its range bucket
for each bucket:
  stable insertion-sort its records
concatenate buckets in increasing range order

Watch for: The main bars remain in place during distribution and inner sorting. The operation text and auxiliary counters show that work; the final collection writes the bars back.

Try Bucket sort (distribution + insertion) in the simulator ↑

Explore Comparison-Based Sorting Visualizer →

GROWTH & COST MODEL
Counting: Θ(n + k) · LSD radix: Θ(d(n + b))

Use the controls above to change the relevant parameters. All displayed values follow the assumptions stated on this page.

TRY IT YOURSELF

Three experiments to try

  1. Compare Counting and Radix on the same eight random keys. Step slowly and read the operation text. Distinguish counting, prefix accumulation, output placement and main-array writes.
  2. Select the One digit at a time preset. Pause after the ones pass has written eight records. Find keys whose tens digits differ and explain why the array is not yet numerically sorted.
  3. Compare Bucket with Counting using many duplicate keys. Open the record readout and check the original-position labels among equal values after completion. Then switch to a random pattern and compare Bucket's key comparisons.
WORKED EXAMPLE

Calculate it before you visualize it

Consider [21, 13, 12, 11]. A stable ones-digit pass groups the two keys ending in 1 in their original order, producing [21, 11, 12, 13]. The tens-digit pass then puts the three teen values first while preserving their ones order: [11, 12, 13, 21]. In this lab, each decimal pass writes all four records back to the main array, so Radix performs eight main writes and zero pairwise key comparisons. Counting needs four main writes, but also maintains 100 key counters. Neither write count alone establishes which implementation is faster on a real computer.

QUICK KNOWLEDGE CHECK

Can you predict the result?

If Counting and Radix make zero key comparisons, should their operation counts and running times also be zero?

Reveal the answer

No. They exploit the structure of keys rather than asking pairwise order questions. They still inspect keys or digits, update counts, compute positions and move records. Comparisons measure only one type of work. The linear-looking formulas include the key range or number of digit passes, which can change the practical cost substantially.

ASSUMPTIONS & LIMITS

What the model does not show

Ascending sort of 8–64 nonnegative integer keys restricted to 0–99. Counting uses k = 100 counters. LSD Radix always uses two stable decimal passes, b = 10 and d = 2. Bucket uses ten fixed-width ranges and stable Insertion inside each bucket. This is not a general sorter for negative numbers, floating-point values, strings or arbitrary user-defined objects. Instrumented event playback is not a wall-clock benchmark.

REAL-WORLD CONNECTIONS

Where sorting is useful

Integer identifiers, bounded categorical keys and fixed-width representations can suit distribution-based techniques. Stable counting is also a building block for radix passes. Real implementations may use bytes rather than decimal digits, process signed values specially or select algorithms based on the key range. Before using a distribution method, establish the domain and memory budget; a short list of very large integers is not automatically a good fit for a huge counter array.

WATCH OUT

Common misconceptions

  • Non-comparison sorting means no work: arithmetic, memory access and record movement still cost time.
  • Counting is always O(n): its range term k must also be counted.
  • Radix works with any unstable digit sorter: later passes need stability to retain earlier digit order.
  • Bucket always has linear expected time: distribution assumptions and bucket count matter.
  • Every distribution method is comparison-free: the insertion sorts inside these buckets compare keys.
CLASSROOM CHALLENGE

Try it with a class

Ask students to draw a small count array for keys 0–4 and compute prefix totals by hand. Have them place equal-key records from right to left into an output array and explain why their identity order is preserved. Next run the four-key radix worked example on paper. For an extension, discuss what changes when keys become signed, the range grows to a billion or the number of buckets remains fixed while the dataset grows.

REFERENCES & MODEL REVIEW

Algorithm sources

MG Labs implements the specific variants described above. Automated tests check sorted order, record preservation, stability and selected exact operation counts. Reference implementations may use different pivots, gaps, bases or optimizations.

MIT OpenCourseWare — Counting sort, radix sort and comparison lower bounds ↗

Princeton — String sorts and LSD radix sorting ↗

Model implementation: MG Labs teaching model. Independent subject-matter review and classroom testing are still pending.