Tutorialspoint.dev

Segregate Even and Odd numbers

Given an array A[], write a function that segregates even and odd numbers. The functions should put all even numbers first, and then odd numbers.

Example:

Input  = {12, 34, 45, 9, 8, 90, 3}
Output = {12, 34, 8, 90, 45, 9, 3}

In the output, the order of numbers can be changed, i.e., in the above example, 34 can come before 12 and 3 can come before 9.


The problem is very similar to our old post Segregate 0s and 1s in an array, and both of these problems are variation of famous Dutch national flag problem.


Algorithm: segregateEvenOdd()
1) Initialize two index variables left and right:  
            left = 0,  right = size -1 
2) Keep incrementing left index until we see an odd number.
3) Keep decrementing right index until we see an even number.
4) If lef < right then swap arr[left] and arr[right]

Implementation:

C/C++

br>
// C program to segregate even and odd elements of array
#include<stdio.h>
  
/* Function to swap *a and *b */
void swap(int *a, int *b);
  
void segregateEvenOdd(int arr[], int size)
{
    /* Initialize left and right indexes */
    int left = 0, right = size-1;
    while (left < right)
    {
        /* Increment left index while we see 0 at left */
        while (arr[left]%2 == 0 && left < right)
            left++;
  
        /* Decrement right index while we see 1 at right */
        while (arr[right]%2 == 1 && left < right)
            right--;
  
        if (left < right)
        {
            /* Swap arr[left] and arr[right]*/
            swap(&arr[left], &arr[right]);
            left++;
            right--;
        }
    }
}
  
/* UTILITY FUNCTIONS */
void swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
  
/* driver program to test */
int main()
{
    int arr[] = {12, 34, 45, 9, 8, 90, 3};
    int arr_size = sizeof(arr)/sizeof(arr[0]);
    int i = 0;
  
    segregateEvenOdd(arr, arr_size);
  
    printf("Array after segregation ");
    for (i = 0; i < arr_size; i++)
        printf("%d ", arr[i]);
  
    return 0;
}

Java

// Java program to segregate even and odd elements of array
import java.io.*;
  
class SegregateOddEven
{
    static void segregateEvenOdd(int arr[])
    {
        /* Initialize left and right indexes */
        int left = 0, right = arr.length - 1;
        while (left < right)
        {
            /* Increment left index while we see 0 at left */
            while (arr[left]%2 == 0 && left < right)
                left++;
  
            /* Decrement right index while we see 1 at right */
            while (arr[right]%2 == 1 && left < right)
                right--;
  
            if (left < right)
            {
                /* Swap arr[left] and arr[right]*/
                int temp = arr[left];
                arr[left] = arr[right];
                arr[right] = temp;
                left++;
                right--;
            }
        }
    }
  
    /* Driver program to test above functions */
    public static void main (String[] args)
    {
        int arr[] = {12, 34, 45, 9, 8, 90, 3};
  
        segregateEvenOdd(arr);
  
        System.out.print("Array after segregation ");
        for (int i = 0; i < arr.length; i++)
            System.out.print(arr[i]+" ");
    }
}
/*This code is contributed by Devesh Agrawal*/

Python

# Python program to segregate even and odd elements of array
  
def segregateEvenOdd(arr):
  
    # Initialize left and right indexes
    left,right = 0,len(arr)-1
  
    while left < right:
  
        # Increment left index while we see 0 at left
        while (arr[left]%2==0 and left < right):
            left += 1
  
        # Decrement right index while we see 1 at right
        while (arr[right]%2 == 1 and left < right):
            right -= 1
  
        if (left < right):
              # Swap arr[left] and arr[right]*/
              arr[left],arr[right] = arr[right],arr[left]
              left += 1
              right = right-1
  
  
# Driver function to test above function
arr = [12, 34, 45, 9, 8, 90, 3]
segregateEvenOdd(arr)
print ("Array after segregation "),
for i in range(0,len(arr)):
    print arr[i],
# This code is contributed by Devesh Agrawal

C#

// C# program to segregate even
// and odd elements of array
using System;
  
class GFG
{
    static void segregateEvenOdd(int []arr)
    {
        /* Initialize left and right indexes */
        int left = 0, right = arr.Length - 1;
        while (left < right)
        {
            /* Increment left index while we see 0 at left */
            while (arr[left]%2 == 0 && left < right)
                left++;
  
            /* Decrement right index while we see 1 at right */
            while (arr[right]%2 == 1 && left < right)
                right--;
  
            if (left < right)
            {
                /* Swap arr[left] and arr[right]*/
                int temp = arr[left];
                arr[left] = arr[right];
                arr[right] = temp;
                left++;
                right--;
            }
        }
    }
  
    /* Driver program to test above functions */
    public static void Main ()
    {
        int []arr = {12, 34, 45, 9, 8, 90, 3};
  
        segregateEvenOdd(arr);
  
        Console.Write("Array after segregation ");
        for (int i = 0; i < arr.Length; i++)
            Console.Write(arr[i]+" ");
    }
}
  
//This code is contributed by Sam007

PHP

<?php
// PHP program to segregate even and
// odd elements of array 
  
function segregateEvenOdd(&$arr, $size
    // Initialize left and right indexes
    $left = 0;
    $right = $size-1; 
    while ($left < $right
    
        // Increment left index while we 
        // see 0 at left
        while ($arr[$left] % 2 == 0 &&
                    $left < $right
            $left++; 
  
        // Decrement right index while we 
        // see 1 at right 
        while ($arr[$right] % 2 == 1 && 
                    $left < $right
            $right--; 
  
        if ($left < $right
        
            // Swap $arr[$left] and $arr[$right]
            swap($arr[$left], $arr[$right]); 
            $left++; 
            $right--; 
        
    
  
// UTILITY FUNCTIONS
function swap(&$a, &$b
    $temp = $a
    $a = $b
    $b = $temp
  
// Driver Code
$arr = array(12, 34, 45, 9, 8, 90, 3); 
$arr_size = count($arr); 
  
segregateEvenOdd($arr, $arr_size); 
  
echo "Array after segregation "
for ($i = 0; $i < $arr_size; $i++) 
    echo $arr[$i]." "
  
// This code is contributed 
// by rathbhupendra
?>

This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter