1: Why create aux?
Because the mergeSort method requires a source and target array.
2: How does this type work if the code sorts aux?
Since the mergeSort method mergeSort sorted from aux to a
3: Isn't it a waste of resources to clone an array before sorting?
No, this is not ... using this implementation of mergeSort . Now, if sort returned a sorted array, doing cloning (rather than creating an empty array) would be wasteful. But the API requires it to do the sorting in place, which means that a must be a "destination". Thus, elements must be copied to a temporary array, which will be the "source".
If you look at the mergeSort method, you will see that it recursively splits the array to be sorted, combining back and forth between its source and target arrays. For this you need two arrays. Presumably, Sun / Oracle has determined that this algorithm provides good performance for typical use cases of Java sorting.
source share