Sorting & Selection Easy
Greedy Sort for Maximum Arrangement
Sort both teams by height, choose one whole team for each row, and pair players by sorted position. Every back-row player must be strictly taller.
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) visualization identities; JDK sort implementation details are external
Array state
Follow the active comparisons and writes.
Current operation
Trace stateReady to explore
Source behavior & common mistakes
Find the row orientation used by the source, reject the first pair that violates strict height ordering, or return the Java int sum of all aligned height differences.
- The source sorts both teams before choosing which team is in front.
- It chooses orientation by comparing only the shortest sorted players; if those heights tie, team A stays in front and the first pair fails.
- The back-row height must be strictly greater; equality returns -1 immediately.
- The height difference and running sum use Java int arithmetic and can overflow even when every pair is valid.
- Java sorts primitive int arrays in place. Stable player tokens in this trace only identify input occurrences; equal-height token order is not part of the source's observable behavior.
Algorithm code
JavaPhoto.javaStep linked
The highlighted line follows the current step.
Ready to begin
Press Play to see what happens.
Detailed execution
Snapshot 1 / 1