4

I'm trying to understand how Java Collections Framework sorts its collections by default and I got confused, because I read all the collections are being sorted using merge sort. But as I took a look at Array class I saw this: «Implementors should feel free to substitute other algorithms, so long as the specification itself is adhered to. (For example, the algorithm used bysort(Object[]) does not have to be a mergesort, but it does have to be stable.)» Which means it also uses other sorting algorithms. So how exactly are the collections being sorted?

2
  • 2
    Unless you are implementing Arrays class yourself, it is using merge sort. Also, Arrays is not the same as Collections Commented Mar 19, 2013 at 15:52
  • 2
    Collections.sort delegates to Arrays.sort, which uses TimSort these days. Commented Mar 19, 2013 at 16:44

1 Answer 1

5

The code to sort collections is delivered with the JRE/JDK.

Anyone who implements the JRE/JDK can choose to implement it in any way he wants, as long as it's conforming (i.e. it actually sorts the collection correctly and the sorting is stable).

Some implementations might choose merge-sort, others might choose something else. No specific implementation is required.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.