Quicksort vs Merge sort in Java

Gerald Nguyen
Gerald Nguyen
2 min read ยท
Previous | Next
On this page

The title is a little larger that what I have in this entry :D

If you want the answer fast, go here http://www.coderanch.com/t/520171/java/java/Why-Collections-sort-merge-sort#2355668

And here is a longer write-up:

I participated in an thread in Javaranche regarding why Arrays uses quicksort and Collections use merge sort (http://www.coderanch.com/t/520171/java/java/Why-Collections-sort-merge-sort). The thread starter claimed that quicksort is the best sort algorithm, for which I disagreed. I also replied with a link to wikipedia about merge sort performs better when the underlying datatype does not support random access (as does array). I thought that was the reason why Collections use merge sort: because a collection type such as LinkedList does not necessarily support random access.

I had some doubt then, though. I did noticed “This implementation dumps the specified list into an array, sorts the array” in the javadoc and wondered why wasn’t quicksort be used. But lazy me doesn’t want to do further googling and reading; I replied with the right-but-was-not-correct arguments.

Later, I checked that thread again and found an answer from a user named “Martin Vajsar”. His explanation is brilliant, but let me rephrase them here:

There is another bonus information from Martin: “Mergesort’s ability to efficiently sort sequentially accessed data is of no real use in Java. This feature is used when sorting data so large that they don’t fit to memory and must be written to disk”