Five ways to put things in order
Sorting rearranges items by a chosen value. Here, the goal is ascending numerical order. Switch algorithms to replay the same input; compare sorted, reversed, and repeated values.
Cyan: idle · Pink: inspected · Amber outline: pivot · Green: final position
Labels such as #3 record original input positions. Equal values can have different identities. A highlighted range is currently being processed; a sorted prefix can still move later.
- Comparisons
- 0
- Swaps
- 0
- Array writes
- 0
- Recursion depth
- 0
Current array
| Index | Value | Original position |
|---|
Merge buffer
L and R identify the copied halves. Faded items have already been written.
Sorting pseudocode
Highlighted lines correspond to recorded operations. Counters measure comparisons between item values and writes to the main array; copying the merge buffer is excluded.
Read the executable JavaScriptExplore the lessons
- Bubble sort — Compare neighbors and let large values travel to the right.
- Selection sort — Find the smallest remaining item and place it in the next slot.
- Insertion sort — Grow a sorted prefix by moving each new item into place.
- Merge sort — Sort two halves, then weave their values into one ordered sequence.
- Quicksort — Partition around a pivot, then sort the ranges on either side.
Small input, revealing behavior
Insertion sort can fix a nearly sorted input with very little work. Selection sort scans the remaining items regardless of their order. Merge sort buys predictable performance with temporary storage. Quicksort depends on its pivot and partition rule: this version deliberately uses the last item, making its worst case visible on sorted and all-equal inputs.
Watch equal values’ original-position labels to explore stability. Reset playback returns to the beginning; switching algorithms retains your values. Shuffle changes the input order and assigns fresh identity labels.