So, there are two different methods with totally different algorithms here:
Arrays.sort(int[]) uses a dual-pivot quicksort algorithm.
Collections.sort(List<T>) calls list.sort(null) which in turn calls Arrays.sort(T[]). This uses a Timsort algorithm.
So, let’s compare Arrays.sort(int[]) and Arrays.sort(T[]).
T[]is a boxed array so there is one extra level of indirection: for each call, you have to unwrap Integer. This certainly leads to an overhead. On the other hand,int[]is a primitive array so all elements are available “immediately”.- TimSort is a variation of a classic mergesort algorithm. It is faster than mergesort but it still slower than quicksort because
- quicksort has fewer data movements on random data
- quicksort requires
O(log(n))extra space while TimSort requiresO(n)to provide stability which also leads to an overhead.