ToolSphere

Sorting Algorithm Visualizer

Computer science

Trace bubble, insertion, selection, merge, quick and heap sort step by step. Compare passes, swaps, key comparisons, stability and space complexity.

Enter a short list and follow how each algorithm puts it in order.

Sorted from smallest to largest

1, 3, 3, 5, 8

10 comparisons · 7 swaps · 14 array writes

Negatives, decimals and duplicates are welcome. Up to 20 numbers; no empty comma entries.

Left-to-right passes with early exit after a pass with no swaps.

Stable
Yes
In place
Yes
Extra space
O(1)
Best time
O(n)
Average time
O(n²)
Worst time
O(n²)

In place refers to array storage. Quick sort still uses a recursion stack. Trace snapshots are extra teaching storage, excluded from these space bounds. Quick sort averages assume random input order.

Step 1 of 6

Starting array

  1. 5
  2. 3
  3. 8
  4. 3
  5. 1

Counts are cumulative. Only comparisons between keys count. A swap exchanges two different positions and makes two writes; self-swaps are skipped. Writes from saved keys or merge buffers count separately. Original-position tags identify ties; temporary writes can briefly repeat a tag while a key is held outside the array.

Array after each pass, merge or partition
StepActionArrayComparisonsSwapsWrites
Starting array5, 3, 8, 3, 1000
Pass 1: suffix from position 5 is sorted3, 5, 3, 1, 8436
Pass 2: suffix from position 4 is sorted3, 3, 1, 5, 87510
Pass 3: suffix from position 3 is sorted3, 1, 3, 5, 89612
Pass 4: suffix from position 2 is sorted1, 3, 3, 5, 810714
Sorted array1, 3, 3, 5, 810714

On this browser only

Give feedback

Did this tool help?

Tell us what worked, what didn’t, or what you’d like next.

Opens Google Forms in a new tab with this tool’s name filled in. No sign-in or email required. Feedback is optional.

How to use Sorting Algorithm Visualizer

  1. 1Start with the loaded example or enter up to 20 numbers separated by commas or spaces.
  2. 2Choose an algorithm, including Lomuto or Hoare quick sort. The sorted result and total counts update above the inputs.
  3. 3Read the pass table, or choose All operations to see every key comparison, swap and array write.
  4. 4Use Previous step, Next step or a table step number to inspect an array. Show original positions to follow equal keys.

About Sorting Algorithm Visualizer

Follow a sorting algorithm step by step

Use this sorting algorithm visualizer to check how a short list becomes sorted from smallest to largest. The loaded example gives you an answer immediately. Change the numbers or choose a different algorithm to compare the work. The page includes bubble, insertion, selection, merge, heap and quick sort, with separate Lomuto and Hoare partition choices. Everything runs in your browser without submitting the input to a server.

This is a study tool for data structures and Intro to Algorithms homework. It shows exact operations for the implementations described on this page. It is not a runtime benchmark or a prediction of how a programming language’s built-in sorting function behaves. Before comparing a trace with your class notes, check the pivot rule, tie handling and definition of a comparison.

Read the result and trace table

The first answer contains the sorted list and total key comparisons, swaps and array writes. Below the inputs, pass summaries show the array after an outer pass, insertion, merge, partition or heap stage. These are different kinds of milestones, so their row counts are not directly comparable across algorithms. Start and finish rows bracket the trace, even when the input contains only a single value.

Choose All operations to inspect every comparison, swap and write. The counts in each row are cumulative through that operation. Use the step buttons or select a numbered row to inspect its array above the table. Highlighted positions show array locations involved in that operation. Comparisons involving temporary merge buffers identify the compared values in the action text instead. On narrow screens, scroll inside the table to read its remaining columns.

What the operation counts mean

A key comparison compares two numeric values. Loop conditions, index checks and recursion stopping conditions do not count. A swap exchanges items at different array positions and contributes two array writes. An attempted exchange with the same position is skipped. Exchanging equal values at different positions still counts as a swap because their original identities can change order.

Insertion sort shifts larger values and writes a saved key into the opening; those actions are writes rather than swaps. Merge sort writes values from temporary buffers back into the array and also reports no swaps. Buffer copies are excluded from the write counter. Zero swaps therefore does not imply zero work. Use comparisons and writes together when explaining these algorithms, and avoid comparing only the swap totals.

Worked example computed by this library

The loaded input is 5, 3, 8, 3, 1. Bubble sort finishes at 1, 3, 3, 5, 8 with 10 comparisons, 7 swaps and 14 array writes. Its first pass produces 3, 5, 3, 1, 8. The next produces 3, 3, 1, 5, 8, followed by 3, 1, 3, 5, 8 and then the sorted list. Each left-to-right sweep moves the largest remaining value to the end of the unsorted region.

For that same input, insertion sort makes 9 comparisons and 11 writes, with no swaps. Selection sort makes 10 comparisons and 3 swaps. Merge sort makes 7 comparisons and 12 writes, with no swaps. Lomuto quick sort makes 8 comparisons and 3 swaps, while Hoare quick sort makes 20 comparisons and 4 swaps. Heap sort makes 9 comparisons and 7 swaps. These totals describe this list and these conventions, not a universal ranking of the algorithms.

Pivot choices and stopping rules matter

Bubble sort stops early after a sweep with no exchanges. On the already sorted example 1, 3, 3, 5, 8, it needs only 4 comparisons and no swaps. Selection sort still scans the entire remaining suffix to find its first minimum. Insertion sort stops scanning backward when it reaches a value no larger than its saved key.

Lomuto uses the last value as the pivot and puts keys less than or equal to it on the left. Hoare saves the middle-position value, scans inward with strict comparisons and splits where the scans meet. The comparison that stops each scan counts. A Hoare split does not promise that the pivot is fixed at its final index. Both versions process the left partition first and use no random shuffle.

Stability, memory and complexity

A stable sort preserves the original relative order of equal keys. Turn on original-position tags to follow duplicates. Bubble, insertion and this merge implementation are stable. Selection, quick and heap sort are not generally stable, even if a particular trace happens to preserve equal keys. Intermediate insertion or merge writes can temporarily repeat an identity while another item is held outside the array.

The summary reports algorithmic time and auxiliary space, excluding the teaching trace itself. In-place quick sort still needs a recursion stack; merge sort needs temporary array storage. Quick sort’s average bound assumes random input order, while unlucky pivots can cause quadratic work. Heap sort has a linear best case for all-equal keys in this implementation. Small operation totals cannot establish asymptotic complexity. Continue in the Algorithms Lab to study recurrences, hashing and coding alongside sorting.

Guides and tutorials

Frequently asked questions

Which sorting algorithms can I trace?

Bubble, insertion, selection, merge, heap and quick sort are available. Quick sort has separate Lomuto and Hoare partition options with their pivot rules shown.

What counts as a comparison?

Only a comparison between numeric keys counts. Index checks, loop conditions and recursion stopping tests are excluded. Hoare includes the key comparison that stops each inward scan.

Why do insertion and merge sort show zero swaps?

These implementations move values through shifts or temporary buffers rather than exchanging two array positions. Read the array-write counter to see those moves.

Does a swap with the same position count?

No. Self-swaps are skipped. Exchanging two different positions counts once, even when their numeric values are equal, and contributes two array writes.

How do Lomuto and Hoare quick sort differ?

Lomuto uses the last key and fixes its pivot at a final index. Hoare uses the middle-position key value and inward scans; its split is a boundary, not necessarily the pivot’s final position.

Which algorithms are stable?

The bubble, insertion and merge implementations preserve equal-key order. Selection, quick and heap are not generally stable. Original-position tags let you track duplicates.

What does in place mean here?

It refers to sorting with constant extra array storage. Quick sort still uses a recursion stack, and merge sort uses temporary arrays. Stored trace snapshots are excluded from the algorithm’s space summary.

Can I enter negative numbers or duplicates?

Yes. Enter up to 20 finite decimal numbers between −1,000,000,000 and 1,000,000,000 using commas or whitespace. Scientific notation and empty comma entries are not accepted.

Why does my homework trace have different counts?

Your course may use different pivots, tie rules, bubble stopping rules, swap definitions or comparison conventions. Check the convention displayed for the selected algorithm before comparing totals.

Can I see every operation instead of only passes?

Yes. Choose All operations to show every key comparison, swap and array write. Counts are cumulative. Select a step number or use the Previous step and Next step buttons to inspect it.

Have a suggestion for Sorting Algorithm Visualizer?Share your feedback ↗