Separate even and odd numbers in an array in Java

Separate even numbers and odd numbers in an array in Java

Given an array[], write a function that segregates even and odd numbers. The functions we are going to create should put all even numbers first, and then odd numbers. Here we will learn to separate even and odd numbers in an array in Java.

In this Java tutorial, we are going to find a function that segregates even and odd numbers without considering order. We are going to use the quick-sort algorithm in this problem.

What is Quick-sort algorithm?

Quick-sort is also a Divide and Conquer algorithm like merge-sort. It partitions the given array along a picked pivot element. There are different ways of choosing the pivot element we can choose any element as a pivot element.

In Quick-sort algorithm pivoted element is made to put in its correct position. Such that all element before the pivot

element should be less than it and all elements after pivot greater than it.

Similarly, in this problem, we can take an element as the pivot and put all even numbers before it and all odd numbers after it

Algorithm:

The logic for this problem is similar to Quick-sort:

  1. Initialize two index variables left=0 and right=n-1
  2. Keep incrementing left index until we get an odd number.
  3. Keep decrementing right index until we get an even number.
  4. If left < right then swap a[left] < a[right] in our program

   Java code to segregate even and odd numbers in an array

import java.util.Scanner;
import java.util.*;

class Codespeedy
{
     public static void segregate(int a[] , int n)
     {
         int left=0 , right=n-1 ,i;
         
           while(left<right)
           {
               while(a[left]%2==0 && left<right)
                    left++;
                    
               while(a[right]%2==1 && left<right)
                    right--;
                    
                    if(left<right)
                    {
                        int temp=a[left];
                         a[left]=a[right];
                         a[right]=temp;
                         left++ ; right--;
                    }
           }
         
     }    
         public static void main(String[] args)
      {
          Scanner scan = new Scanner(System.in);
          
             int n = scan.nextInt();
             
               int a[] = new int[1000];
               
               for(int i=0;i<n;i++)
               {
                   int x=scan.nextInt();
                     a[i]=x;
               }
             
             segregate(a,n);
             
             for(int i=0;i<n;i++)
               System.out.print(a[i]+" ");
      }
    
}

Example:

Input

12 34 45 9 8 90 3

Output

12 34 90 8 9 45 3

Similarly, using this algorithm we can solve segregating 0 and 1 in an array type problem. These problems are variations of the famous Dutch national flag problem.

Also, read:

I hope this tutorial on How to separate even and odd numbers in Java will help you to understand the logic behind achieving our goal.

 

One response to “Separate even and odd numbers in an array in Java”

  1. Aadarsh says:

    Nice explained

Leave a Reply

Your email address will not be published. Required fields are marked *