File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Number of comparison in sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Number of comparison in sorting" Watch "Number of comparison in sorting" New topic
Author

Number of comparison in sorting

Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1281

In Java we do perform sorting using comparable or comparator interfaces and for similar objects it can be done by Collection.sort() method. I wanted to know, for say if we have 5 objects to sort, how many comparison will going to take place? is it !n or !n-1. also wanted to know, is there any default implementation of compareTo and compare methods? if so what algorithm is used by it and how to compression of objects take place?

Thanks


No Kaustubh No Fun, Know Kaustubh Know Fun..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42951
    
  72
The fastest way to get answers to these is probably to look it up in the Java source code that comes with the JDK in a file called src.zip or src.jar.
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1281

I did that ulf but I am not able to understand what Collections.sort() functioning, One thing I don't understand is we pass collection object from Collections.sort() method to compareTo() method that we override and from there we compare this.value to collectionObject.value... means we comparing what from what??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19074
    
  40

Kaustubh G Sharma wrote:In Java we do perform sorting using comparable or comparator interfaces and for similar objects it can be done by Collection.sort() method. I wanted to know, for say if we have 5 objects to sort, how many comparison will going to take place? is it !n or !n-1. also wanted to know, is there any default implementation of compareTo and compare methods? if so what algorithm is used by it and how to compression of objects take place?


This can't be answer exactly -- certainly not if the question is "is it N or N-1 comparisons?", meaning specific to an exact count. From the JavaDoc, the algorithm is a modified merge sort. And for those who studied what a merge sort is, it has an order of "N Log N", but ...

1. "N Log N" is only the complexity. It may be relative to the number of comparisons, and even then, it makes the assumption that N approaches infinity. So, you can get a relative scale, but not really an exact count.

2. With the merge sort, the "N Log N" is only a cap. It is actually data dependent. I believe merge sort needs less comparisons for a set of data that is more "sorted" than a completely random set of data.

3. The modified part of the merge sort will actually cause more comparisons, but depending on the result of the comparison, it may actually cause less comparisons. In other words, a more sorted set of data may trigger short cuts which may need less comparisons.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1281

Wooooo... Henry to understand this, will take years for me..I am very bad in mathematics I wrote a demo code for sorting 5 custom class objects using comparable interface and also keeping track of no. comparisons and I've got compareTo() method finish to run in 5 times.. how it does that?
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 908
    
    5
The number of the comparisions depends on both the algorithm and the input data. As an extreme, you can sort N objects using N-1 comparisions: obviously if they happen to be in the ideal sequence, comparing them in their order lets you check with N-1 comparisions that they are already sorted.

Scott Shipp
Ranch Hand

Joined: Mar 31, 2013
Posts: 142
    
    7

Just want to note that in Java 7, the merge sort algorithm previously used has been replaced with the "dual pivot quicksort" by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch for primitives and by TimSort for objects (since it is stable and quicksort is not). The information available for the dual pivot quicksort is exciting if you are interested in analysis of algorithms. Google it if you are interested.
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1281

Scott Shipp wrote:Just want to note that in Java 7, the merge sort algorithm previously used has been replaced with the "dual pivot quicksort" by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch for primitives and by TimSort for objects (since it is stable and quicksort is not). The information available for the dual pivot quicksort is exciting if you are interested in analysis of algorithms. Google it if you are interested.


Interesting information. Thanks Scott
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Number of comparison in sorting