AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

sorting int[] in descending order

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Vahe Musoyan
Guest





PostPosted: Fri Oct 27, 2006 8:05 pm    Post subject: sorting int[] in descending order Reply with quote



What's the best way to sort an array of integers in descending order?
I know that one can sort in ascending order and reverse the array. Is
there any other/better way?
Back to top
Patricia Shanahan
Guest





PostPosted: Fri Oct 27, 2006 8:20 pm    Post subject: Re: sorting int[] in descending order Reply with quote



Vahe Musoyan wrote:
Quote:
What's the best way to sort an array of integers in descending order?
I know that one can sort in ascending order and reverse the array. Is
there any other/better way?


There are other ways, but I don't think any of the ones I know are better:

1. Write your own quicksort descending order implementation.

2. Create an Integer array with the values from the int[] and
Arrays.sort it using a supplied Comparator that reverses the
a.compareTo(b) result.

3. Replace each element of the array with the result of subtracting it
from -1, sort normally, then repeat the replacement operation. The
initial subtraction maps the largest value to the smallest, smallest to
largest etc. The final subtraction restores the original values.

Option 1 involves a lot of coding. Options 2 and 3 each involve more
overhead than reversing the array after an ascending order sort.

If, for its other uses, this array is on the borderline between being
best represented by an int[] or an Integer[], then the descending sort
requirement might tip it over to Integer[], and you could do the sort
with descending order Comparator without any copying.

Patricia
Back to top
Eric Sosman
Guest





PostPosted: Fri Oct 27, 2006 8:37 pm    Post subject: Re: sorting int[] in descending order Reply with quote



Patricia Shanahan wrote On 10/27/06 11:20,:
Quote:
Vahe Musoyan wrote:

What's the best way to sort an array of integers in descending order?
I know that one can sort in ascending order and reverse the array. Is
there any other/better way?



There are other ways, but I don't think any of the ones I know are better:

1. Write your own quicksort descending order implementation.

2. Create an Integer array with the values from the int[] and
Arrays.sort it using a supplied Comparator that reverses the
a.compareTo(b) result.

3. Replace each element of the array with the result of subtracting it
from -1, sort normally, then repeat the replacement operation. The
initial subtraction maps the largest value to the smallest, smallest to
largest etc. The final subtraction restores the original values.
[...]

4. "Count backwards:" sort the array in ascending order, but
"reflect" the index whenever you access it. Most crudely,
replace array[i] with array[array.length-1-i]. Somewhat more
smoothly, replace

for (int i = 0; i < array.length; ++i)

with

for (int i = array.length; --i >= 0; )

--
Eric.Sosman (AT) sun (DOT) com
Back to top
Daniel Dyer
Guest





PostPosted: Sat Oct 28, 2006 10:35 pm    Post subject: Re: sorting int[] in descending order Reply with quote

On Fri, 27 Oct 2006 16:20:23 +0100, Patricia Shanahan <pats (AT) acm (DOT) org> wrote:

Quote:
2. Create an Integer array with the values from the int[] and
Arrays.sort it using a supplied Comparator that reverses the
a.compareTo(b) result.

If you choose to use this method you can make use of
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html#reverseOrder().

Dan.

--
Daniel Dyer
http://www.uncommons.org
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.