Sorting Algorithm Simulator
Step through eight sorting algorithms on entered or seeded arrays with verified order, stability and operation counts for each deterministic run.Current trace
{{ summaryLine }}
Run review
What to notice
{{ algorithmFacts.guidance }}
Verification: {{ completionCheck }}
The chart renderer is unavailable; the same counts remain in the run ledger.
| Signal | Value | Meaning | Copy |
|---|---|---|---|
| {{ row.label }} | {{ row.value }} | {{ row.detail }} |
Sorting has two correctness requirements. The final values must appear in the requested order, and they must still be the same items that were present at the start. A sequence that looks ordered but loses, duplicates, or changes an item is not a valid sort.
Algorithms reach that result through different kinds of work. Bubble, selection, and insertion sort build order through local comparisons and movement. Merge and quick sort divide the array into smaller ranges. Heap sort maintains a heap before each extraction, while counting and radix sort use value structure instead of pairwise comparison as their main mechanism.
| Family | Algorithms | Useful observation |
|---|---|---|
| Pass and prefix methods | Bubble, selection, insertion | Watch a completed edge or sorted prefix grow. |
| Divide and combine | Merge, quick | Follow recursive ranges, buffered merges, or pivot partitions. |
| Priority structure | Heap | See the root repaired before each extreme value is extracted. |
| Distribution methods | Counting, radix | Track frequencies or digit buckets rather than comparison decisions. |
Stability is a separate property from correctness. A stable sort keeps equal-valued items in their original relative order. This matters when values stand for records that also contain names, timestamps, or earlier sort keys. Two arrays can display the same numbers while differing in the identities attached to duplicates.
Operation counts explain one trace, not the speed of a programming language or computer. Comparisons, swaps, and writes have different costs in real systems, and auxiliary memory is not summarized by those counts. Fair comparisons keep the exact same array, identities, direction, and counting rules.
How to Use This Tool:
Choose the data and sort order first, then compare algorithms with the same values and playback settings.
- Choose one of the eight algorithms and set Order to ascending or descending.
- For Entered values, provide 1 to 16 whole numbers from -99 through 99. For Generated values, choose the size and arrangement and keep the same seed when comparing algorithms.
- If counting sort reports a range error, reduce the inclusive span from the smallest value to the largest to 100 integer slots or fewer.
- Select Start simulation, then use Step to examine each comparison, swap, write, partition, heap repair, count, or digit pass. Finish moves to the same deterministic terminal frame.
- Use Autoplay and Playback speed only for presentation. They change timing, not the trace, counts, or final order.
- Confirm the completed state in Run review, then compare the Operation mix and Run ledger when every setup value is the same.
Interpreting Results:
The completion check verifies both requested order and the original item identities. This catches a trace that appears sorted but has changed the multiset of input items. With duplicates, identity order also reveals whether the selected algorithm preserved stability.
- Comparisons count explicit ordering checks.
- Swaps count pair exchanges; each exchange also contributes two writes.
- Writes count assignments back into the working array, including shifts, merges, reconstruction, and digit passes.
A lower count in one category is not a universal win. Counting and radix sort can report no pairwise comparisons while using frequency or bucket storage. Merge sort can make more writes while retaining predictable growth. Use the ledger to explain the work instead of treating one total as a benchmark.
Technical Details:
The canonical trace stores the value and original identity at every frame. Completion requires adjacent values to satisfy the selected ascending or descending comparison and requires every final identity to map back to its original value.
Rule Core:
| Algorithm | Trace mechanism | Stability | Growth summary |
|---|---|---|---|
| Bubble | Compare adjacent pairs; swap an out-of-order pair; stop early after a pass with no swap. | Stable | O(n2) average and worst case |
| Selection | Scan the unsorted range for the next best item and swap it to the boundary. | Unstable | O(n2) comparisons |
| Insertion | Shift larger items in the sorted prefix and insert the incoming key after equal items. | Stable | O(n) best case; O(n2) worst case |
| Merge | Split ranges in half and stably merge buffered fronts. | Stable | O(n log n) time; O(n) auxiliary space |
| Quick | Use the last item as pivot, partition the range, then recurse on both sides. | Unstable | O(n log n) average; O(n2) worst case |
| Heap | Build a heap, move the root to the completed suffix, and repair the remaining heap. | Unstable | O(n log n) time |
| Counting | Count signed values across a bounded range and rewrite equal items in original identity order. | Stable | O(n + k) time and space |
| Radix | Normalize signed keys and perform stable least-significant base-10 digit passes. | Stable | O(d(n + 10)) |
Mechanism and Verification:
Ascending comparison treats a smaller value as earlier; descending comparison reverses that decision. Equal values compare as equal. Stable paths choose the earlier buffered item, insert after equals, or reconstruct equal values in original identity order.
| Detail | Exact behavior |
|---|---|
| Manual input | 1 to 16 integers, each from -99 through 99. |
| Generated input | A seed deterministically produces 1 to 16 values from -40 through 40 before the selected arrangement is applied. |
| Counting range | Maximum minus minimum plus 1 must be at most 100. |
| Quick-sort pivot | The last item of each active range is the pivot. |
| Radix keys | Ascending subtracts the minimum; descending subtracts each value from the maximum before base-10 passes. |
| Terminal invariant | Values are ordered and final identities are exactly the original identity-value pairs. |
The displayed complexity labels describe conventional growth for these algorithm forms. The recorded operation totals belong only to the bounded trace and exclude browser rendering, animation timing, chart work, and memory-allocation costs.
Worked Examples:
One bubble-sort exchange
Sort 2, 1 in ascending order with bubble sort. The trace compares the two values, swaps them, and verifies 1, 2. The completed run records one comparison, one swap, two writes, and three frames including the final verification.
References:
- Sort, NIST Dictionary of Algorithms and Data Structures.
- OpenDSA Data Structures and Algorithms Modules Collection, OpenDSA Project.