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.