GenerateRandomSearch

Sorting Algorithm Trace Generator

Three classic sorting algorithms, traced pass by pass on the same kind of input, with the counts that actually tell them apart. Selection sort always makes the same number of comparisons whatever the input; insertion sort is cheap on nearly-sorted data and expensive on reversed; bubble sort swaps once per inversion. Those differences are the point, and they are measured here rather than described.

What this generator does

Runs the chosen algorithm on a random array and records the array after every outer pass, along with the number of comparisons and the number of data moves. Both counts are recomputed by a second run before display.

How to use this tool

  1. Choose the algorithm and how many values to sort.
  2. Work through the passes yourself, or generate and compare.
  3. Reveal the answer for the full trace and the counts.
  4. Seed the array to give two people the identical exercise.

Understanding the controls

How many values to sort
Between 4 and 12. Twelve values already produce sixty-six comparisons for the quadratic algorithms, which is enough to make the point.
Algorithm
Bubble compares adjacent pairs, insertion shifts each value back into place, and selection finds the smallest remaining value each pass. The counts differ sharply.
Seed
Reproduces the same array, so the same exercise can be set twice or compared across algorithms.
Show the answer
Reveals the array after each pass along with the totals.

Common use cases

  • Worked traces for teaching, with the intermediate states written out
  • Comparing comparison counts across the three algorithms on the same size of input
  • Showing that selection sort never gets lucky, whatever the input
  • Checking a hand-worked trace against the real one
  • Producing the same array twice for a class exercise

How this generator works

Each algorithm is implemented plainly and instrumented as it runs. The check does two things a sortedness test alone would miss: it confirms the result is a permutation of the input, so no value was lost or invented, and it re-runs the trace to confirm the counts.

Randomness and fairness

The array is random; the trace and the counts are determined by it and the algorithm. Seeded arrays reproduce exactly and are therefore explicitly not cryptographically secure. Unseeded, the browser's cryptographically secure generator fills the array.

For how randomness is produced across the whole site, see how Generate Random works.

Limitations and good to know

  • Three quadratic algorithms only. Merge sort, quicksort and heapsort trace very differently and are not offered.
  • The trace records one line per outer pass, not per comparison, which keeps it readable at the cost of detail.
  • Values may repeat, so stability is visible in principle but is not called out.
  • There is no animation — this is a written trace.
  • Traces are not stored between visits.

Privacy and your data

Arrays and traces are generated in your browser and never transmitted. Nothing survives the visit.