Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting Of Array From Even Number To Odd Number

 
Deepak Sahu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5563
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could consider implementing a Comparator<Integer> and then calling Arrays.sort(T[] a, Comparator<? super T> c).
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But yeah, Stephan's way is the "proper" way of doing it.
 
Stephan van Hulst
Bartender
Pie
Posts: 5563
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...?
 
Stephan van Hulst
Bartender
Pie
Posts: 5563
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or along the same lines without using ArrayList:

 
Stephan van Hulst
Bartender
Pie
Posts: 5563
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic