File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

duplicate values in sort

 
Senaca Boyd
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have an array that is not sorted and contains duplicate values, which of these sorting techniques will successfully accomplish my sort: heap sort, selection sort, or bubble sort?
[ April 05, 2007: Message edited by: Senaca Boyd ]
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do you mean by "successfully accomplish my sort"? all of these techniques will sort the array.

could you be more specific in your question?
 
Senaca Boyd
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought there were some sort techniques that would not work if one of the values being sorted was a duplicate. Am I wrong?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't remember which of these are which, but there is the concept of a 'stable' vs. 'unstable' sort.

this concept relates to how you handle two items with the same key. in a stable sort, the pre-sort order of items with the same key is preserved. an unstable sort makes no garantees either way.

this is more of an issue when you are sorting on multiple fields - say first and last name. if i sort by first name first, when i then sort by last name, i want to keep all the smiths in the order they came to me.

I don't know if that is what you were thining of or not. but if you are sorting on a single element, the sort algorithm shouldn't matter.

some may be more or less efficient, but again, i don't think that is what you were aiming towards
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic