Sorting & Selection Medium
Top-Down Merge Sort
Top-down merge sort recursively divides an array, sorts each half, and merges the two sorted ranges through an auxiliary array.
Trace input
Bounded JSONEdit the lesson input, then build a deterministic source-linked trace.
Source scenarios
Complexity
BestO(n log n)
AverageO(n log n)
WorstO(n log n)
SpaceO(n)
Array state
Follow the active comparisons and writes.
Current operation
Trace stateReady to explore
Source behavior & common mistakes
Sort signed 32-bit integers in ascending order while following the exact recursive calls and each write to the auxiliary and original arrays.
- The split is inclusive: the left half ends at mid and the right half begins at mid + 1.
- Each merge copies the whole current range to aux before writing back into a.
- When compareTo returns zero, this implementation takes the left item, which makes the merge stable.
- The recursive base case is hi <= lo, including empty ranges such as 0…-1.
Algorithm code
JavaMergeSort.javaStep linked
The highlighted line follows the current step.
Ready to begin
Press Play to see what happens.
Detailed execution
Snapshot 1 / 1
Comparisons 0 · Writes 0