w3resource

Java Array Exercises: Separate even and odd numbers of a given array of integers

Java Array: Exercise-52 with Solution

Write a Java program to separate even and odd numbers of a given array of integers. Put all even numbers first, and then odd numbers.

Pictorial Presentation:

Java Array Exercises: Separate even and odd numbers of a given array of integers

Sample Solution:

Java Code:

import java.util.Arrays;
public class Main
{
	
	 public static void main (String[] args)
     {
        int nums[] = {20, 12, 23, 17, 7, 8, 10, 2, 1, 0};
        int result[];
        System.out.println("Original Array ");
        System.out.println(Arrays.toString(nums));

        result = separate_odd_even(nums);
 
        System.out.print("Array after separation ");
        System.out.println(Arrays.toString(result));
    }
	
    static int [] separate_odd_even(int arr[])
    {
        int left_side = 0, right_side = arr.length - 1;
        while (left_side < right_side)
        {
            while (arr[left_side]%2 == 0 && left_side < right_side)
                left_side++;
 
            while (arr[right_side]%2 == 1 && left_side < right_side)
                right_side--;
 
            if (left_side < right_side)
            {
                int temp = arr[left_side];
                arr[left_side] = arr[right_side];
                arr[right_side] = temp;
                left_side++;
                right_side--;
            }
        }
        return arr;
    }
}

Sample Output:

                                                                              
Original Array 
[20, 12, 23, 17, 7, 8, 10, 2, 1, 0]
Array after separation [20, 12, 0, 2, 10, 8, 7, 17, 1, 23]

Flowchart:

Flowchart: Separate even and odd numbers of a given array of integers

Visualize Java code execution (Python Tutor):


Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to separate 0s on left side and 1s on right side of an array of 0s and 1s in random order.
Next: Write a Java program to replace every element with the next greatest element (from right side) in a given array of integers.

What is the difficulty level of this exercise?



Java: Tips of the Day

Array vs ArrayLists:

The main difference between these two is that an Array is of fixed size so once you have created an Array you cannot change it but the ArrayList is not of fixed size. You can create instances of ArrayLists without specifying its size. So if you create such instances of an ArrayList without specifying its size Java will create an instance of an ArrayList of default size.

Once an ArrayList is full it re-sizes itself. In fact, an ArrayList is internally supported by an array. So when an ArrayList is resized it will slow down its performance a bit as the contents of the old Array must be copied to a new Array.

At the same time, it's compulsory to specify the size of an Array directly or indirectly while creating it. And also Arrays can store both primitives and objects while ArrayLists only can store objects.

Ref: https://bit.ly/3o8L2KH