Cost of sorting in Java
What happens when you call Collections.sort(myList)
, how do you think? It is obvious, that efficient sorting algorithm will be applied, isn't it? Not so easy.
First, list.toArray()
method is called. Depending on which one of the two most common List
implementations you use: ArrayList
or LinkedList
, it costs array creation and either efficient System.arraycopy()
call in the case of ArrayList
or list iteration and one-by-one element copying in the case of LinkedList
.
Next, the array will be sorted using Arrays.sort(Object[])
method. Guess, what happens there. Another array of the same size is created, elements are copied into it (pretty efficiently though), and mergesort is performed on it recursively.
In the end, elements are copied from the sorted array into initial List
one by one.
Grand total cost is 2 array creations and sorting cost equal to O(n*log(n))
. Don't forget about recursive calls.
Now suppose, that system under load has to perform dozens of such sorts per second on an array of size about 20000. This becomes a bottleneck.
The solution is easy: write custom sorting procedure and perform quicksort on your ArrayList
or consider to use another container, e.g. SortedSet
. Arrays.sort()
for arrays of primitive types doesn't suffer of redundant array creation. It merely uses quicksort.
For the future: know you libs.
Best of luck!