aspose file tools
The moose likes Java in General and the fly likes Why Collections.sort is using merge sort insteadof quicksort? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Why Collections.sort is using merge sort insteadof quicksort?" Watch "Why Collections.sort is using merge sort insteadof quicksort?" New topic
Author

Why Collections.sort is using merge sort insteadof quicksort?

Thennam Pandian
Ranch Hand

Joined: Oct 11, 2005
Posts: 163
Hi,

We know that quick sort is the best sorting algorithm.

The collections.sort used merge sort algorithm instead of quick sort. But Arrays.sort uses quick sort.

What is the reason Collections.sort uses merge sort instead of quick sort?

T. Huy Nguyen
Ranch Hand

Joined: Nov 02, 2010
Posts: 57
Hi,

I'm not sure if quicksort is the best sorting algo. Merge sort is also very fast. And I found this in wikipedia: "Merge sort is more efficient than quick sort for some types of lists if the data to be sorted can only be efficiently accessed sequentially".

Anyway, since quicksort may be better than merge sort when operating on a array-like data type, it is used implemented in Arrays. But when the assumption about array-like data type cannot be made (as in the case of Collection), merge sort seems like a safer bet


My material for SCJP (2008), SCWCD (2010), SCBCD (2010). About me
Thennam Pandian
Ranch Hand

Joined: Oct 11, 2005
Posts: 163
Hi,

We can't measure the performance of an sorting algorithm based on "how we access the elements".

Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

Thennam Pandian wrote:We can't measure the performance of an sorting algorithm based on "how we access the elements".

Why not?
Martin Vajsar
Bartender

Joined: Aug 22, 2010
Posts: 2321
    
    2

The main reason why mergesort is used in Collections.sort is that mergesort is stable - it does not reorder elements that are equal. Look up "stable sort" on Wikipedia if you don't understand the consequences.

On the other hand, Arrays.sort is used to sort primitives. Here the stability of the sort is meaningless, as you cannot distinguish two values that are equal. Hence, quicksort is used (except when sorting an array of Objects, for which mergesort is performed). Moreover, quicksort can be done in place, so there is no need to allocate another array.

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. Even if you sort a linked list, which cannot be accessed randomly, Java creates an array-based copy, sorts it and repopulates the list from this copy.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: Why Collections.sort is using merge sort insteadof quicksort?
 
Similar Threads
sorting of a collection of Java classes
Question regarding Sorting
how to use quick sort algo
Quicksort vs Merge Sort
Sorting Algorithm in Collections