jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Sorting Of Array From Even Number To Odd Number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting Of Array From Even Number To Odd Number" Watch "Sorting Of Array From Even Number To Odd Number" New topic
Author

Sorting Of Array From Even Number To Odd Number

Deepak Sahu
Greenhorn

Joined: Dec 08, 2009
Posts: 16
Hi Friends ,

Please any one help to sort the below array from even to odd .

I have a array like

int a[] ={11 ,34 ,23 ,8,90,5} ;

The requirement is we have to sort the array like


int []={34,8,90.11,23,5};
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

You could consider implementing a Comparator<Integer> and then calling Arrays.sort(T[] a, Comparator<? super T> c).
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

I don't think there's a built-in command to do that. You'd have to go through the elements and use



then copy all the odd and even numbers into a new array.

OR, you could turn the array into an ArrayList and go through the list, testing if each element is even, and if so, moving it to the start of the list. Probably easier.

Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

But yeah, Stephan's way is the "proper" way of doing it.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

The only problem is that you would have to convert the int[] to an Integer[] first, which is sadly a bit ugly and maybe costly.
Anyway, here's a comparator you could use.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Actually, I'm not sure there's a "proper" way here.

Using a Comparator still requires that you write the logic to implement, and there's the overhead of creating Integer Objects for each int, then getting primitive ints back again.

My guess is that Deepak's question is more about how to approach the logic. As Luigi suggested, one approach is to separate even and odd, then treat them as separate cases. Any other ideas...?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

Hmmm here's another example, based on what Luigi suggested:
This should actually be more efficient than the Arrays.sort() method, because that uses O(n*log(n)) time, while this uses O(n) time.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Or along the same lines without using ArrayList:

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

Luigi, you may not want to use i % 2 == 1 to decide whether a number is odd. It will not work for negative values.

Use i % 2 != 0 instead.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Be careful if you use Comparator<Integer>. Because remember that if you compare Integers (not int's) you're comparing objects. The Integer class has an Integer cache so the return values may be unexpected. A way to "solve" this is to make them int's:



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Wouter Oet wrote:The Integer class has an Integer cache so the return values may be unexpected.

What sort of unexpected values??
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Wouter means using ==. For all Integer objects not retrieved from the auto-boxing cache that will return false even if their values are the same:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

I understand. You should use the equals() method for objects - don't have the to turn them into ints, but it would be easy to use == by mistake and think your code was working if you only tested with low numbers.

I just did a test and the same thing happens with the auto-boxing in Arraylists... this one's more confusing because Integer objects aren't even mentioned:

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Luigi Plinge wrote:...the same thing happens with the auto-boxing in Arraylists... this one's more confusing because Integer objects aren't even mentioned...

Unlike arrays, Collections (like Lists) cannot hold primitive values. This is why the autoboxing happens.

Although this will compile and run without mentioning Integer types, you will probably see compiler warnings about unchecked or unsafe operations. Since Java 5, the safer way is to specify a type for the Collection...

...this allows the compiler to insert all the necessary casting and avoid surprises about what your Collection actually contains.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting Of Array From Even Number To Odd Number
 
Similar Threads
Help! Sorting problem
Flexibly sorting primitive arrays
loops
print out random number and state odd/even
Avoiding if else loop