The reason for this is quite simple: the complexity of the solution time with Integer better.
Sounds weird, right?
Arrays.sort uses double sliding sorting for primitives, which works O(N^2) times in the worst case. In the test case, your solution fails - it is a specially developed anti-slip sorting method.
However, the version for objects uses merge sorting, which works in O(N * log N) for any possible input.
Note that it is not part of the Java language specification (it does not say how the sort method should be implemented), but it works like in most real implementations (for example, this is the case for openjdk-8 )
PS Such things happen more or less often in competitive programming, so I recommend either sorting arrays of objects, or using collections.
kraskevich
source share