w3resource

Java: 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 from 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 the necessary Java utility class for working with arrays.
import java.util.Arrays;

// Define the Main class.
public class Main {
	
    public static void main(String[] args)
    {
        // Create an array of integers.
        int nums[] = {20, 12, 23, 17, 7, 8, 10, 2, 1, 0};
        int result[];
        
        // Print the original array.
        System.out.println("Original Array ");
        System.out.println(Arrays.toString(nums));

        // Call the separate_odd_even method to separate odd and even numbers.
        result = separate_odd_even(nums);
 
        // Print the array after separation.
        System.out.print("Array after separation ");
        System.out.println(Arrays.toString(result));
    }
	
    // A method to separate odd and even numbers in an array.
    static int[] separate_odd_even(int arr[])
    {
        // Initialize left and right pointers.
        int left_side = 0, right_side = arr.length - 1;
        
        // Continue until the left pointer is less than the right pointer.
        while (left_side < right_side)
        {
            // Move the left pointer to the right until it points to an odd number.
            while (arr[left_side] % 2 == 0 && left_side < right_side)
                left_side++;
 
            // Move the right pointer to the left until it points to an even number.
            while (arr[right_side] % 2 == 1 && left_side < right_side)
                right_side--;
 
            // If the left pointer is still less than the right pointer, swap the elements.
            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 the modified array.
        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

Java Code Editor:

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?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/array/java-array-exercise-52.php