• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what sorting algorithm comparator,comparable does?

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai,

Do we have to know what sorting algorithm comparable Interface compareTo() and comparator Interface compare() does for SCJP 5 Exam ?


I Appreciate your valuable time and response

samura
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since these are interfaces, the methods are abstract. The API specifies that they should be implemented to return "a negative integer, zero, or a positive integer" under certain conditions -- but exactly how this is done depends on the implementing class.

Sorting algorithms are not part of the SCJP exam.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorting algorithms are not part of the SCJP, but what Comparable and Comparator are and how you use them might be part of it. (I'm not sure, lookup the SCJP exam objectives and check it yourself).
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The K&B-book deals with the Comparable and Comparator interface and when my memories are right. I had a comparator question in the exam. I also think knowledge of the implementation of these interfaces is important when you are designing your own classes and want to use these within collections, etc.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There really is no sorting algorithm in any implementation of Comparable / compareTo() or Comparator / compare(). The algorithm would be in Collections.sort(), Arrays.sort(), TreeSet, TreeMap, and other classes that use Comparable/Comparator. And in any event, you don't need to know anything about what the algorithm is.
[ July 07, 2007: Message edited by: Jim Yingst ]
 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



dvdinfo.txt file

Donnie Darko/sci-fi/Gyllenhall, Jake
Raiders of the Lost Ark/action/Ford, Harrison
2001/sci-fi/??
Caddy Shack/comedy/Murray, Bill
Star Wars/sci-fi/Ford, Harrison
Lost in Translation/comedy/Murray, Bill
Patriot Games/action/Ford, Harrison


for my understanding compare() i added print statements (which is
more confusing)

output as follows: may I know what soring algorithm is use in Collection.sort(dvdInfoList,gs)

oneTITLE: Donnie Darko GENRE:sci-fi Actor :Gyllenhall, Jake

twoTITLE: Raiders of the Lost Ark GENRE:action Actor :Ford, Harrison

oneTITLE: Donnie Darko GENRE:sci-fi Actor :Gyllenhall, Jake

twoTITLE: 2001 GENRE:sci-fi Actor :??

oneTITLE: Caddy Shack GENRE:comedy Actor :Murray, Bill

twoTITLE: Star Wars GENRE:sci-fi Actor :Ford, Harrison

oneTITLE: Star Wars GENRE:sci-fi Actor :Ford, Harrison

twoTITLE: Lost in Translation GENRE:comedy Actor :Murray, Bill

oneTITLE: Caddy Shack GENRE:comedy Actor :Murray, Bill

twoTITLE: Lost in Translation GENRE:comedy Actor :Murray, Bill

oneTITLE: Star Wars GENRE:sci-fi Actor :Ford, Harrison

twoTITLE: Patriot Games GENRE:action Actor :Ford, Harrison

oneTITLE: Lost in Translation GENRE:comedy Actor :Murray, Bill

twoTITLE: Patriot Games GENRE:action Actor :Ford, Harrison

oneTITLE: Caddy Shack GENRE:comedy Actor :Murray, Bill

twoTITLE: Patriot Games GENRE:action Actor :Ford, Harrison

oneTITLE: 2001 GENRE:sci-fi Actor :??

twoTITLE: Patriot Games GENRE:action Actor :Ford, Harrison

oneTITLE: Raiders of the Lost Ark GENRE:action Actor :Ford, Harrison

twoTITLE: Patriot Games GENRE:action Actor :Ford, Harrison

oneTITLE: Donnie Darko GENRE:sci-fi Actor :Gyllenhall, Jake

twoTITLE: Patriot Games GENRE:action Actor :Ford, Harrison

oneTITLE: Donnie Darko GENRE:sci-fi Actor :Gyllenhall, Jake

twoTITLE: Caddy Shack GENRE:comedy Actor :Murray, Bill

oneTITLE: Donnie Darko GENRE:sci-fi Actor :Gyllenhall, Jake

twoTITLE: Lost in Translation GENRE:comedy Actor :Murray, Bill

oneTITLE: Donnie Darko GENRE:sci-fi Actor :Gyllenhall, Jake

twoTITLE: Star Wars GENRE:sci-fi Actor :Ford, Harrison

oneTITLE: 2001 GENRE:sci-fi Actor :??

twoTITLE: Star Wars GENRE:sci-fi Actor :Ford, Harrison


Thanks in advance
samura


 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, as start you can look at the documentation for Collections.sort().

The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n log(n) performance. This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place.



See also:
Merge Sort
reply
    Bookmark Topic Watch Topic
  • New Topic