kirillbobyrev
kirillbobyrev

Reputation: 1729

Java Collections Framework's sorting algorithms

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?

Upvotes: 4

Views: 926

Answers (1)

Joachim Sauer
Joachim Sauer

Reputation: 308001

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.

Upvotes: 5

Related Questions