This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Machine Learning with R: Expert techniques for predictive modeling and have Brett Lantz on-line!
See this thread for details.
Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Confused about comparator logic

 
Ranch Hand
Posts: 108
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am confused about the comparator sorting logic. When the list is incrementing, i can understand how it was going but when the difference of two elements in comparison is negative, i cant get the logic of its operation.

Please help me in getting it understanding









Result:

/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=64576:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/lib/tools.jar:/Users/magantimurthy/Documents/izo809/out/production/izo809 com.izo809.collections.Comparator.Simple
Sorting by Name
s1 = Student{rollno=6, name='d', age=20}
s2 = Student{rollno=7, name='b', age=50}
<<<<<>>>>>>
s1 = Student{rollno=5, name='a', age=33}
s2 = Student{rollno=6, name='d', age=20}
<<<<<>>>>>>
s1 = Student{rollno=5, name='a', age=33}
s2 = Student{rollno=6, name='d', age=20}
<<<<<>>>>>>
s1 = Student{rollno=5, name='a', age=33}
s2 = Student{rollno=7, name='b', age=50}
<<<<<>>>>>>
s1 = Student{rollno=4, name='e', age=14}
s2 = Student{rollno=7, name='b', age=50}
<<<<<>>>>>>
s1 = Student{rollno=4, name='e', age=14}
s2 = Student{rollno=6, name='d', age=20}
<<<<<>>>>>>
s1 = Student{rollno=3, name='g', age=52}
s2 = Student{rollno=6, name='d', age=20}
<<<<<>>>>>>
s1 = Student{rollno=3, name='g', age=52}
s2 = Student{rollno=4, name='e', age=14}
<<<<<>>>>>>
s1 = Student{rollno=2, name='f', age=16}
s2 = Student{rollno=6, name='d', age=20}
<<<<<>>>>>>
s1 = Student{rollno=2, name='f', age=16}
s2 = Student{rollno=3, name='g', age=52}
<<<<<>>>>>>
s1 = Student{rollno=2, name='f', age=16}
s2 = Student{rollno=4, name='e', age=14}
<<<<<>>>>>>
s1 = Student{rollno=1, name='c', age=71}
s2 = Student{rollno=4, name='e', age=14}
<<<<<>>>>>>
s1 = Student{rollno=1, name='c', age=71}
s2 = Student{rollno=7, name='b', age=50}
<<<<<>>>>>>
s1 = Student{rollno=1, name='c', age=71}
s2 = Student{rollno=6, name='d', age=20}
<<<<<>>>>>>
5 a 33
7 b 50
1 c 71
6 d 20
4 e 14
2 f 16
3 g 52
Sorting by age
Before sorting = [Student{rollno=5, name='a', age=33}, Student{rollno=7, name='b', age=50}, Student{rollno=1, name='c', age=71}, Student{rollno=6, name='d', age=20}, Student{rollno=4, name='e', age=14}, Student{rollno=2, name='f', age=16}, Student{rollno=3, name='g', age=52}]
s1 = Student{rollno=7, name='b', age=50}
s2 = Student{rollno=5, name='a', age=33}
********
s1 = Student{rollno=1, name='c', age=71}
s2 = Student{rollno=7, name='b', age=50}
********
s1 = Student{rollno=6, name='d', age=20}
s2 = Student{rollno=1, name='c', age=71}
********
s1 = Student{rollno=6, name='d', age=20}
s2 = Student{rollno=7, name='b', age=50}
********
s1 = Student{rollno=6, name='d', age=20}
s2 = Student{rollno=5, name='a', age=33}
********
s1 = Student{rollno=4, name='e', age=14}
s2 = Student{rollno=7, name='b', age=50}
********
s1 = Student{rollno=4, name='e', age=14}
s2 = Student{rollno=5, name='a', age=33}
********
s1 = Student{rollno=4, name='e', age=14}
s2 = Student{rollno=6, name='d', age=20}
********
s1 = Student{rollno=2, name='f', age=16}
s2 = Student{rollno=5, name='a', age=33}
********
s1 = Student{rollno=2, name='f', age=16}
s2 = Student{rollno=6, name='d', age=20}
********
s1 = Student{rollno=2, name='f', age=16}
s2 = Student{rollno=4, name='e', age=14}
********
s1 = Student{rollno=3, name='g', age=52}
s2 = Student{rollno=5, name='a', age=33}
********
s1 = Student{rollno=3, name='g', age=52}
s2 = Student{rollno=1, name='c', age=71}
********
s1 = Student{rollno=3, name='g', age=52}
s2 = Student{rollno=7, name='b', age=50}
********
4 e 14
2 f 16
6 d 20
5 a 33
7 b 50
3 g 52
1 c 71

Process finished with exit code 0


I tried to analyse the code but not understanding how its comparing

My notes:

Sortinb by age

Original list:

33,50,71,20,14,16,52

s1=50
s2=33
s1>s2 , it goes to next

s1=71
s2=50
s1>s2


whenever s1>s2 its understandable but what is happening when s1<s2

s1=20
s2=71


s1=20
s2=50


s1=20
s2=33

***********
s1=14
s2=50

s1=14
s2=33

s1=14
s2=20
*************

Could any one help me to understand what is happening when s1<s2.

Thanks

Swapna
 
Marshal
Posts: 7185
492
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took this as an example from javadocs, so what it says:

public static int compare​(long x, long y)

Compares two long values numerically. The value returned is identical to what would be returned by:
   Long.valueOf(x).compareTo(Long.valueOf(y))

Parameters:
x - the first long to compare
y - the second long to compare

Returns:
the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y


Read the returns part. Don't you get that part how the sorting logic is defined? So the return value basically just denotes, wheter the given two elements supposed to be swapped or not.
 
Bartender
Posts: 3519
150
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Swapna,

the method compare(a, b) can return three values:
1, meaning a is larger than b
0, meaning a and b are equal in this comparison
-1, meanong a is smaller than b

Your nameComparator compares the students names, in alphabetical order, ageComparator compares two students on their age.
So, if student a = 40, and student b = 36, then -1 is returned, and then the comparator knows that b is smaller than student a.

You can write the Comparators in a much shorter way, but then you's mss the print statements.
 
Liutauras Vilda
Marshal
Posts: 7185
492
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swapna latha wrote:...but when the difference of two elements in comparison is negative, i cant get the logic of its operation


If the return value is negative (less than 0), then such elements are not being swapped, because they are in order already (as per documentation).
 
Swapna latha
Ranch Hand
Posts: 108
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Swapna latha wrote:...but when the difference of two elements in comparison is negative, i cant get the logic of its operation


If the return value is negative (less than 0), then such elements are not being swapped, because they are in order already (as per documentation).



When i ran with program



The output is:
/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=57359:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/lib/tools.jar:/Users/magantimurthy/Documents/izo809/out/production/izo809 com.izo809.collections.Comparator.Simple
7 b 20
6 d 50
5 a 33
Sorting by age
Before sorting = [Student{rollno=7, name='b', age=20}, Student{rollno=6, name='d', age=50}, Student{rollno=5, name='a', age=33}]
s1 = Student{rollno=6, name='d', age=50}
s2 = Student{rollno=7, name='b', age=20}
********
s1 = Student{rollno=5, name='a', age=33}
s2 = Student{rollno=6, name='d', age=50}
********
s1 = Student{rollno=5, name='a', age=33}
s2 = Student{rollno=6, name='d', age=50}
********
s1 = Student{rollno=5, name='a', age=33}
s2 = Student{rollno=7, name='b', age=20}
********
7 b 20
5 a 33
6 d 50

Process finished with exit code 0


Why do i get s1=33 compared with 50 and 20 , that is why s1=33 for two times. Confused here.
 
Marshal
Posts: 14060
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd have to understand what Collections.sort actually does, what sorting algorithm it uses. Why does it matter to you though? I don't see why this should be a concern.
 
Swapna latha
Ranch Hand
Posts: 108
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:You'd have to understand what Collections.sort actually does, what sorting algorithm it uses. Why does it matter to you though? I don't see why this should be a concern.



I just want to understand what exactly the sorting logic is really doing.

Thanks

Swapna
 
Junilu Lacar
Marshal
Posts: 14060
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swapna latha wrote:I just want to understand what exactly the sorting logic is really doing.


That's an implementation detail and could be different for different Java implementations/versions.

You can start with the JavaDocs. The official API documentation for Collections.sort() has this note:

Implementation Note: This implementation defers to the List.sort(Comparator) method using the specified list and comparator.

If you really want to analyze the algorithm, you'd have to find the source code and dig around in that. But again, the source code you dig into may or may not be that of the implementation you're using. Again, I wouldn't really worry about that too much. It's not something most programmers have to care about unless performance is a big issue. The standard libraries are usually optimized for average usage and often perform well enough under extreme conditions, too. If you're just learning Java, I would focus more on mastering language elements and good coding style.
 
Bartender
Posts: 2372
102
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I understand what you mean. The easiest way I can explain it is by writing a simple code that does bubble sort.

See the program below:
Are you able to understand this code ?
 
Liutauras Vilda
Marshal
Posts: 7185
492
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swapna latha wrote:Why do i get s1=33 compared with 50 and 20 , that is why s1=33 for two times. Confused here.


Alright, now I see what you mean. Agree, it isn't obvious why it is so. To me too. By looking to documentation of list.sort(), it provides some implemetation note which you can read, there is also the reference link to some variation of TimSort, which seems to be adopted here, so perhaps you could gather some kowledge from both of these links.

Try to find some visualization how both of these algorithms work, maybe you could find an answer in there. But most likely the implementation isn't exact standard of common sorting algorithms, so trying to understand how it actually works is a bit pointless exercise as been mentioned before.

Revising how most common sorting algorithms work perhaps is enough to have some understanding about them.
 
Swapna latha
Ranch Hand
Posts: 108
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all. Its great to see all legends here. Once again , thank you all.
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!