Java Program to Separate all Even Numbers First and Then Odd Numbers

In the previous article, we have seen Java Program to Separate 0s on Left Side and 1s on Right Side of an Array of 0s and 1s in Random Order

In this article we are going to see how to separate all even numbers first and then odd numbers using Java programming language.

Java Program to Separate all Even Numbers First and Then Odd Numbers

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to separate all even numbers first and then odd numbers.

Method-1: Java Program to Separate all Even Numbers First and Then Odd Numbers By Static Initialization of Array Elements

Approach:

  • Initialize two pointers, i = 0, j = arr.length – 1.
  • While i <= j, if the element at i is even, increment i.
  • If the element at j is odd, decrement j.
  • Now at index i, there is an even element and at index j, there is an odd element, so swap these two.

Program:

public class Main 
{
    public static void main(String[] args) 
    {
        // initialize the array
        int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        // print the array
        System.out.print("Original array: ");
        for (int i : nums)
        {
            System.out.print(i+ " ");
        }
        // call the method
        placeNumber(nums);
        // print the array
        System.out.print("\nModified array: ");
        for (int i : nums) 
        {
            System.out.print(i + " ");
        }
    }

    //placeNumber() method to keep all even numbers first 
    //then all negative number in array
    static void placeNumber(int[] nums) 
    {
        int i = 0;
        int j = nums.length - 1;
        while (i <= j) 
        {
            // if the element at i is negative, increment i
            if (nums[i] % 2 == 0 )
                i++;
            // if the element at j is positive, increment j
            if (nums[j] % 2== 1)
                j--;
            // swap the elements
            if (i <= j) 
            {
                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
            }
        }
    }
}
Output:

Original array: 1 2 3 4 5 6 7 8 9 10 
Modified array: 10 2 8 4 6 5 7 3 9 1

Method-2: Java Program to Separate all Even Numbers First and Then Odd Numbers By Dynamic Initialization of Array Elements

Approach:

  • Ask use length of the array.
  • Initialize the array with given size.
  • Ask the user for array elements.
  • Initialize two pointers, i = 0, j = arr.length – 1.
  • While i <= j, if the element at i is even, increment i.
  • If the element at j is odd, decrement j.
  • Now at index i, there is an even element and at index j, there is an odd element, so swap these two.

Program:

import java.util.*;

public class Main 
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        // asking user to enter the number of elements
        System.out.println("Enter number of elements in the array: ");
        int n = sc.nextInt();
        // initializing the array
        int[] nums = new int[n];
        // asking user to enter the elements
        System.out.println("Enter elements of the array: ");
        for (int i = 0; i < n; i++) {
            nums[i] = sc.nextInt();
        }

        // print the array
        System.out.println("Original array: ");
        for (int i : nums)
        {
            System.out.print(i+ " ");
        }
        // call the method
        placeNumber(nums);
        // print the array
        System.out.println("\nModified array: ");
        for (int i : nums) 
        {
            System.out.print(i + " ");
        }
    }

    //placeNumber() method to keep all even numbers first 
    //then all negative number in array
    static void placeNumber(int[] nums) 
    {
        int i = 0;
        int j = nums.length - 1;
        while (i <= j) 
        {
            // if the element at i is negative, increment i
            if (nums[i] % 2 == 0 )
                i++;
            // if the element at j is positive, increment j
            if (nums[j] % 2== 1)
                j--;
            // swap the elements
            if (i <= j) 
            {
                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
            }
        }
    }
}
Output:

Enter number of elements in the array: 
5
Enter elements of the array:
2 3 1 5 6 
Original array: 
2 3 1 5 6 
Modified array: 
2 6 5 1 3

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Related Java Programs: