In Java, I usually provide both options (when writing reusable utility methods):
public List<T> mergeSort(Collection<T extends Comparable<T>> col); public void mergeSortIn(List<T extends Comparable<T>> col);
I make some assumptions about signatures and types here. However, the Java norm - or at least was * - was usually for state change. This is often dangerous, especially across the borders of the API β for example, changing the collection passed to your library by its βclientβ code. Minimizing the overall state and the mutable state in particular is often a sign of a well-designed application / library.
It looks like you want to reuse the same test data. To do this, I would write a method that creates test data and returns them. That way, if I need the same test data again in another test (i.e., to test the mergeSort () / insertionSort () implementations on the same data), you simply create and return it again. I usually do just that when writing unit tests (e.g. in JUnit).
In any case, if your code is a library class / method for use by other people, you should clearly document its behavior.
In addition: in the "real" code, there should be no reason to indicate that merge sorting is the implementation used. The caller should take care of what he does, not how he does it - so the name will usually not be mergeSort (), insertionSort (), etc.
(*) In some new JVM languages, there has been a conscious movement from mutable data. Clojure does not have any mutable state at all, as it is a pure functional programming language (at least in normal single-threaded application development). Scala provides a parallel set of collection libraries that do not change the state of collections. This has great advantages in multi-threaded, multi-processor applications. It is not as expensive as expected due to the smart algorithms used by the collections.
Paul source share