Sorting Train

Compare neighboring numbered carriages and swap them into sorted order.

Grades 3 to 5 · Computer Science

Save resource to class

Loading your classrooms…

Sign in to save

Bookmark this page so you can find it later.

LivePhysics staff accounts use their usual sign-in provider below.

Sign in to save

Bookmark this page so you can find it later.

LivePhysics staff accounts use their usual sign-in provider below.

Report an Error

Reported page · Simulation

Sorting Train

/simulations/sorting-train/

This helps us reproduce what you saw. Clear the link to leave it out.

or drop them here

Up to 3 images or PDFs, 5 MB per file. 0 of 3 attached.

Explore one idea

Sorting Train

Your goal

Put the carriages in nondecreasing order, smallest first. Select neighboring positions, compare them, then swap when needed. Equal values may stay together.

1 of 3
Current train order: 3, 1, 2, 1. Comparisons: 0. Swaps: 0. Compare the selected pair before swapping. Sorting yard • neighboring comparisons3Position 11Position 22Position 31Position 4Comparisons: 0 • Swaps: 0 • Equal numbers keep their value

Reduced motion is on. The controls still update the scene.

Current train order: 3, 1, 2, 1. Comparisons: 0. Swaps: 0. Compare the selected pair before swapping.

Neighbor positions

Only pressing Compare increases the comparison counter. Only a legal swap increases the swap counter. There is no automatic sorting. Try another strategy after resetting and compare its actual work.

Learn with Sorting Train

Sorting Train brings an algorithm into a familiar railway yard. Numbered carriages arrive out of order, including repeated values that belong next to each other in the finished train. Select neighboring positions, compare their numbers, and decide whether a swap helps. Each deliberate comparison and legal swap contributes to its own counter, so students can compare the work required by different strategies. Repeated left to right passes offer one useful approach. Resetting the same train makes it possible to try another strategy and explain how its sequence of decisions differs.

What to look for

Compare first

Select two neighboring positions and compare before swapping.

Keep equal values

Nondecreasing order allows equal values side by side without a swap.

Count actual work

Compare different strategies using the separate comparison and swap counters.

For teachers and curious learners

Explore adjacent sorting strategies and count the comparisons and swaps performed. Ask students to predict what will happen, change one setting, and explain what they observe. Learn gives a starting point. Practice and Challenge offer new goals with the same scene.

Discrete adjacent compare and swap model with up to 200 operations. Counters are reconstructed from a validated operation history; duplicate values remain distinct carriages with equal numbers.

← All simulations