Sorting & Selection Hard
Column Aggregation Sort
Aggregate stably sorts rows by one chosen column, then combines each contiguous key group into a row of per-column modes.
Trace input
Bounded JSONEdit the lesson input, then build a deterministic source-linked trace.
Source scenarios
Complexity
BestO(N log N + N·M)
AverageO(N log N + N·M)
WorstO(N log N + N·M) expected with HashMap
SpaceO(N·M)
Array state
Follow the active comparisons and writes.
Current operation
Trace stateReady to explore
Source behavior & common mistakes
Follow the selected-column sort, detect each group boundary, count each column's values with a frequency map, and append the resulting mode row. Tied modes use the smallest integer.
- The grouping key is selected by column index; the output keeps the original column order.
- Each group output cell is a mode, not the first row's value or a sum.
- A frequency tie chooses the smaller integer, including when values are negative.
- Groups appear in ascending order of the selected column.
- Arrays.sort on the Java object array is stable, so rows with equal keys retain their input order while they are scanned.
- The upstream method sorts the supplied input array in place; this deterministic trace keeps a private working copy so replay does not mutate caller input.
- The upstream implementation reads input[0], so this lesson requires a nonempty, rectangular matrix and a valid column.
Algorithm code
JavaAggregate.javaStep linked
The highlighted line follows the current step.
Ready to begin
Press Play to see what happens.
Detailed execution
Snapshot 1 / 1