• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Comparable and Comparator

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

The two interfaces are used for object sorting. well, i want to know they follow which sorting mechanism(algorithm). i guess insertion sorting. please can anyone give light on this ?
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Link to Collections class API. Look up the sort() methods there.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am not mentioned about which order(natural order or ascending order) they are sorted. i am about sorting technique.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are just interfaces, which allows to determine the natural order of the compared objects. The comparing algorithm is in the class doing the actual comparision. For example, java.util.Collections.sort(). The API says : 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 n2log(n) performance that would result from attempting to sort a linked list in place.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well Chrishtope, but merge sort needs two list[or array] , then it will sort and merge those two into one list. but here i have only one list which has the objects need to be sort. in this case you mean Collections class (sort()) use the merge sort?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but here i have only one list which has the objects need to be sort.

Collections.sort() uses only one list, the list you want to sort.

in this case you mean Collections class (sort()) use the merge sort?

Yes.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Christophe
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic