# Segregate even and odd numbers in array

Given an array of integer elements, Our goal is to organize the array using even and odd numbers. The order of the resultant array is form of subarrays that contain even and odd numbers. Write a program to segregate array elements. For example.

``````Example
arr = [1, 8, 7, 3, 9, 4, 2, 5, 10]

// After segregate
output = [8, 4, 2, 10,  9, 1, 7, 5, 3]

Here even  [8, 4, 2, 10]
odd   [9, 1, 7, 5, 3]``````

Note that the resulting even and odd elements order is not significant. But all numbers must be at the beginning of the array and all odd numbers must be at the end of the array.

``````/*
Java Program
Segregate even and odd numbers in array
*/
public class Segregation
{
// Swap array element
public void swapElement(int[] arr, int i, int j)
{
// Get i location element
int temp = arr[i];
// Swap value
arr[i] = arr[j];
arr[j] = temp;
}
// Function which is display array elements
public void display(int[] arr, int size)
{
// iterating the array elements
for (int i = 0; i < size; ++i)
{
System.out.print("  " + arr[i] );
}
System.out.print("\n");
}
// Function which is separating given array
// elements of Even and odd numbers
public void segregateElement(int[] arr, int size)
{

// Just before the modifying array elements
System.out.print("Before Segregating Even and Odd elements \n");
// Display given array
this.display(arr, size);

int j = 0;
// iterating the array elements
for (int i = 0; i < size; ++i)
{
if (arr[i] % 2 == 0)
{
// Swap element values
this.swapElement(arr, j, i);
// Visit to next location.
// Incremented location value by one
j++;
}
}
// After modify array elements
System.out.print("After Segregating Even and Odd elements \n");
this.display(arr, size);
System.out.print("\n");
}
public static void main(String[] args)
{
// Define arrays of integer elements
int[] arr1 = {
1 , 8 , 7 , 3 , 9 , 4 , 2 , 5 , 10
};
int[] arr2 =  {
9 , 8 , 2 , 7 , 6 , 2 , 4 , 9 , 7 , 4
};
// Get the number of elements in arr1
int size = arr1.length;
// Check A
// Get the number of elements in arr2
size = arr2.length;
// Check B
}
}``````

#### Output

``````Before Segregating Even and Odd elements
1  8  7  3  9  4  2  5  10
After Segregating Even and Odd elements
8  4  2  10  9  1  7  5  3

Before Segregating Even and Odd elements
9  8  2  7  6  2  4  9  7  4
After Segregating Even and Odd elements
8  2  6  2  4  4  9  9  7  7
``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program
Segregate even and odd numbers in array
*/
class Segregation
{
public:
// Swap array element
void swapElement(int arr[], int i, int j)
{
// Get i location element
int temp = arr[i];
// Swap value
arr[i] = arr[j];
arr[j] = temp;
}
// Function which is display array elements
void display(int arr[], int size)
{
// iterating the array elements
for (int i = 0; i < size; ++i)
{
cout << "  " << arr[i];
}
cout << "\n";
}
// Function which is separating given array
// elements of Even and odd numbers
void segregateElement(int arr[], int size)
{
// Just before the modifying array elements
cout << "Before Segregating Even and Odd elements \n";
// Display given array
this->display(arr, size);
int j = 0;
// iterating the array elements
for (int i = 0; i < size; ++i)
{
if (arr[i] % 2 == 0)
{
// Swap element values
this->swapElement(arr, j, i);
// Visit to next location.
// Incremented location value by one
j++;
}
}
// After modify array elements
cout << "After Segregating Even and Odd elements \n";
this->display(arr, size);
cout << "\n";
}
};
int main()
{
// Define arrays of integer elements
int arr1[] = {
1 , 8 , 7 , 3 , 9 , 4 , 2 , 5 , 10
};
int arr2[] = {
9 , 8 , 2 , 7 , 6 , 2 , 4 , 9 , 7 , 4
};
// Get the number of elements in arr1
int size = sizeof(arr1) / sizeof(arr1[0]);
// Check A
// Get the number of elements in arr2
size = sizeof(arr2) / sizeof(arr2[0]);
// Check B
return 0;
}``````

#### Output

``````Before Segregating Even and Odd elements
1  8  7  3  9  4  2  5  10
After Segregating Even and Odd elements
8  4  2  10  9  1  7  5  3

Before Segregating Even and Odd elements
9  8  2  7  6  2  4  9  7  4
After Segregating Even and Odd elements
8  2  6  2  4  4  9  9  7  7
``````
``````// C Program to
// Segregate even and odd numbers in array
#include <stdio.h>
// Function which is swapping two array elements of given location
void swap_element(int arr[], int i, int j)
{
// Get i location element
int temp = arr[i];
// Swap value
arr[i] = arr[j];
arr[j] = temp;
}
// Function which is display array elements
void display(int arr[], int size)
{
// iterating the array elements
for (int i = 0; i < size; ++i)
{
printf("%d ", arr[i]);
}
printf("\n");
}
// Function which is separating given array elements of Even and odd numbers
void segregate_element(int arr[], int size)
{
int even = 0;
// Just before the modifying array elements
printf("Before Segregating Even and Odd elements \n");
// Display given array
display(arr, size);
// iterating the array elements
for (int i = 0; i < size; ++i)
{
if (arr[i] % 2 == 0)
{
// Swap element values
swap_element(arr, even, i);
// Visit to next location.
// Incremented  location value by one
even++;
}
}
// After modify array elements
printf("After Segregating Even and Odd elements \n");
display(arr, size);
printf("\n");
}
int main()
{
// Define arrays of integer elements
int arr1[] = {
1 , 8 , 7 , 3 , 9 , 4 , 2 , 5 , 10
};
int arr2[] = {
9 , 8 , 2 , 7 , 6 , 2 , 4 , 9 , 7 , 4
};
// Get the number of elements
int size = sizeof(arr1) / sizeof(arr1[0]);
// Check A
segregate_element(arr1, size);
// Get the number of elements
size = sizeof(arr2) / sizeof(arr2[0]);
// Check B
segregate_element(arr2, size);
return 0;
}``````

#### Output

``````Before Segregating Even and Odd elements
1 8 7 3 9 4 2 5 10
After Segregating Even and Odd elements
8 4 2 10 9 1 7 5 3

Before Segregating Even and Odd elements
9 8 2 7 6 2 4 9 7 4
After Segregating Even and Odd elements
8 2 6 2 4 4 9 9 7 7
``````
``````package main
import "fmt"
/*
Go Program
Segregate even and odd numbers in array
*/
type Segregation struct {}
func getSegregation() * Segregation {
var me *Segregation = &Segregation {}
return me
}
// Swap array element
func(this Segregation) swapElement(arr[] int, i int, j int) {
// Get i location element
var temp int = arr[i]
// Swap value
arr[i] = arr[j]
arr[j] = temp
}
// Function which is display array elements
func(this Segregation) display(arr[] int, size int) {
// iterating the array elements
for i := 0 ; i < size ; i++ {
fmt.Print("  ", arr[i])
}
fmt.Print("\n")
}
// Function which is separating given array
// elements of Even and odd numbers
func(this Segregation) segregateElement(arr[] int, size int) {
// Just before the modifying array elements
fmt.Print("Before Segregating Even and Odd elements \n")
// Display given array
this.display(arr, size)
var j int = 0
// iterating the array elements
for i := 0 ; i < size ; i++ {
if arr[i] % 2 == 0 {
// Swap element values
this.swapElement(arr, j, i)
// Visit to next location.
// Incremented location value by one
j++
}
}
// After modify array elements
fmt.Print("After Segregating Even and Odd elements \n")
this.display(arr, size)
fmt.Print("\n")
}
func main() {
var task * Segregation = getSegregation()
// Define arrays of integer elements
var arr1 = [] int {1 , 8 , 7 , 3 , 9 , 4 , 2 , 5 , 10}
var arr2 = [] int {9 , 8 , 2 , 7 , 6 , 2 , 4 , 9 , 7 , 4}
// Get the number of elements in arr1
var size int = len(arr1)
// Check A
// Get the number of elements in arr2
size = len(arr2)
// Check B
}``````

#### Output

``````Before Segregating Even and Odd elements
1  8  7  3  9  4  2  5  10
After Segregating Even and Odd elements
8  4  2  10  9  1  7  5  3

Before Segregating Even and Odd elements
9  8  2  7  6  2  4  9  7  4
After Segregating Even and Odd elements
8  2  6  2  4  4  9  9  7  7
``````
``````// Include namespace system
using System;
/*
Csharp Program
Segregate even and odd numbers in array
*/
public class Segregation
{
// Swap array element
public void swapElement(int[] arr, int i, int j)
{
// Get i location element
int temp = arr[i];
// Swap value
arr[i] = arr[j];
arr[j] = temp;
}
// Function which is display array elements
public void display(int[] arr, int size)
{
// iterating the array elements
for (int i = 0; i < size; ++i)
{
Console.Write("  " + arr[i]);
}
Console.Write("\n");
}
// Function which is separating given array
// elements of Even and odd numbers
public void segregateElement(int[] arr, int size)
{
// Just before the modifying array elements
Console.Write("Before Segregating Even and Odd elements \n");
// Display given array
this.display(arr, size);
int j = 0;
// iterating the array elements
for (int i = 0; i < size; ++i)
{
if (arr[i] % 2 == 0)
{
// Swap element values
this.swapElement(arr, j, i);
// Visit to next location.
// Incremented location value by one
j++;
}
}
// After modify array elements
Console.Write("After Segregating Even and Odd elements \n");
this.display(arr, size);
Console.Write("\n");
}
public static void Main(String[] args)
{
// Define arrays of integer elements
int[] arr1 = {
1 , 8 , 7 , 3 , 9 , 4 , 2 , 5 , 10
};
int[] arr2 = {
9 , 8 , 2 , 7 , 6 , 2 , 4 , 9 , 7 , 4
};
// Get the number of elements in arr1
int size = arr1.Length;
// Check A
// Get the number of elements in arr2
size = arr2.Length;
// Check B
}
}``````

#### Output

``````Before Segregating Even and Odd elements
1  8  7  3  9  4  2  5  10
After Segregating Even and Odd elements
8  4  2  10  9  1  7  5  3

Before Segregating Even and Odd elements
9  8  2  7  6  2  4  9  7  4
After Segregating Even and Odd elements
8  2  6  2  4  4  9  9  7  7
``````
``````<?php
/*
Php Program
Segregate even and odd numbers in array
*/
class Segregation
{
// Swap array element
public  function swapElement(&\$arr, \$i, \$j)
{
// Get i location element
\$temp = \$arr[\$i];
// Swap value
\$arr[\$i] = \$arr[\$j];
\$arr[\$j] = \$temp;
}
// Function which is display array elements
public  function display(\$arr, \$size)
{
// iterating the array elements
for (\$i = 0; \$i < \$size; ++\$i)
{
echo("  ".\$arr[\$i]);
}
echo("\n");
}
// Function which is separating given array
// elements of Even and odd numbers
public  function segregateElement(&\$arr, \$size)
{
// Just before the modifying array elements
echo("Before Segregating Even and Odd elements \n");
// Display given array
\$this->display(\$arr, \$size);
\$j = 0;
// iterating the array elements
for (\$i = 0; \$i < \$size; ++\$i)
{
if (\$arr[\$i] % 2 == 0)
{
// Swap element values
\$this->swapElement(\$arr, \$j, \$i);
// Visit to next location.
// Incremented location value by one
\$j++;
}
}
// After modify array elements
echo("After Segregating Even and Odd elements \n");
\$this->display(\$arr, \$size);
echo("\n");
}
}

function main()
{
// Define arrays of integer elements
\$arr1 = array(1, 8, 7, 3, 9, 4, 2, 5, 10);
\$arr2 = array(9, 8, 2, 7, 6, 2, 4, 9, 7, 4);
// Get the number of elements in arr1
\$size = count(\$arr1);
// Check A
// Get the number of elements in arr2
\$size = count(\$arr2);
// Check B
}
main();``````

#### Output

``````Before Segregating Even and Odd elements
1  8  7  3  9  4  2  5  10
After Segregating Even and Odd elements
8  4  2  10  9  1  7  5  3

Before Segregating Even and Odd elements
9  8  2  7  6  2  4  9  7  4
After Segregating Even and Odd elements
8  2  6  2  4  4  9  9  7  7
``````
``````/*
Node JS Program
Segregate even and odd numbers in array
*/
class Segregation
{
// Swap array element
swapElement(arr, i, j)
{
// Get i location element
var temp = arr[i];
// Swap value
arr[i] = arr[j];
arr[j] = temp;
}
// Function which is display array elements
display(arr, size)
{
// iterating the array elements
for (var i = 0; i < size; ++i)
{
process.stdout.write("  " + arr[i]);
}
process.stdout.write("\n");
}
// Function which is separating given array
// elements of Even and odd numbers
segregateElement(arr, size)
{
// Just before the modifying array elements
process.stdout.write("Before Segregating Even and Odd elements \n");
// Display given array
this.display(arr, size);
var j = 0;
// iterating the array elements
for (var i = 0; i < size; ++i)
{
if (arr[i] % 2 == 0)
{
// Swap element values
this.swapElement(arr, j, i);
// Visit to next location.
// Incremented location value by one
j++;
}
}
// After modify array elements
process.stdout.write("After Segregating Even and Odd elements \n");
this.display(arr, size);
process.stdout.write("\n");
}
}

function main()
{
// Define arrays of integer elements
var arr1 = [1, 8, 7, 3, 9, 4, 2, 5, 10];
var arr2 = [9, 8, 2, 7, 6, 2, 4, 9, 7, 4];
// Get the number of elements in arr1
var size = arr1.length;
// Check A
// Get the number of elements in arr2
size = arr2.length;
// Check B
}
main();``````

#### Output

``````Before Segregating Even and Odd elements
1  8  7  3  9  4  2  5  10
After Segregating Even and Odd elements
8  4  2  10  9  1  7  5  3

Before Segregating Even and Odd elements
9  8  2  7  6  2  4  9  7  4
After Segregating Even and Odd elements
8  2  6  2  4  4  9  9  7  7
``````
``````#  Python 3 Program
#  Segregate even and odd numbers in array
class Segregation :
#  Swap list element
def swapElement(self, arr, i, j) :
#  Get i location element
temp = arr[i]
#  Swap value
arr[i] = arr[j]
arr[j] = temp

#  Function which is display list elements
def display(self, arr, size) :
i = 0
#  iterating the list elements
while (i < size) :
print("  ", arr[i], end = "")
i += 1

print(end = "\n")

#  Function which is separating given list
#  elements of Even and odd numbers
def segregateElement(self, arr, size) :
#  Just before the modifying list elements
print("Before Segregating Even and Odd elements ")
#  Display given list
self.display(arr, size)
j = 0
i = 0
#  iterating the list elements
while (i < size) :
if (arr[i] % 2 == 0) :
#  Swap element values
self.swapElement(arr, j, i)
#  Visit to next location.
#  Incremented location value by one
j += 1

i += 1

#  After modify list elements
print("After Segregating Even and Odd elements ")
self.display(arr, size)
print(end = "\n")

def main() :
#  Define lists of integer elements
arr1 = [1, 8, 7, 3, 9, 4, 2, 5, 10]
arr2 = [9, 8, 2, 7, 6, 2, 4, 9, 7, 4]
#  Get the number of elements in arr1
size = len(arr1)
#  Check A
#  Get the number of elements in arr2
size = len(arr2)
#  Check B

if __name__ == "__main__": main()``````

#### Output

``````Before Segregating Even and Odd elements
1   8   7   3   9   4   2   5   10
After Segregating Even and Odd elements
8   4   2   10   9   1   7   5   3

Before Segregating Even and Odd elements
9   8   2   7   6   2   4   9   7   4
After Segregating Even and Odd elements
8   2   6   2   4   4   9   9   7   7
``````
``````#  Ruby Program
#  Segregate even and odd numbers in array
class Segregation
#  Swap array element
def swapElement(arr, i, j)
#  Get i location element
temp = arr[i]
#  Swap value
arr[i] = arr[j]
arr[j] = temp
end

#  Function which is display array elements
def display(arr, size)
i = 0
#  iterating the array elements
while (i < size)
print("  ", arr[i])
i += 1
end

print("\n")
end

#  Function which is separating given array
#  elements of Even and odd numbers
def segregateElement(arr, size)
#  Just before the modifying array elements
print("Before Segregating Even and Odd elements \n")
#  Display given array
self.display(arr, size)
j = 0
i = 0
#  iterating the array elements
while (i < size)
if (arr[i] % 2 == 0)
#  Swap element values
self.swapElement(arr, j, i)
#  Visit to next location.
#  Incremented location value by one
j += 1
end

i += 1
end

#  After modify array elements
print("After Segregating Even and Odd elements \n")
self.display(arr, size)
print("\n")
end

end

def main()
#  Define arrays of integer elements
arr1 = [1, 8, 7, 3, 9, 4, 2, 5, 10]
arr2 = [9, 8, 2, 7, 6, 2, 4, 9, 7, 4]
#  Get the number of elements in arr1
size = arr1.length
#  Check A
#  Get the number of elements in arr2
size = arr2.length
#  Check B
end

main()``````

#### Output

``````Before Segregating Even and Odd elements
1  8  7  3  9  4  2  5  10
After Segregating Even and Odd elements
8  4  2  10  9  1  7  5  3

Before Segregating Even and Odd elements
9  8  2  7  6  2  4  9  7  4
After Segregating Even and Odd elements
8  2  6  2  4  4  9  9  7  7

``````
``````/*
Scala Program
Segregate even and odd numbers in array
*/
class Segregation()
{
// Swap array element
def swapElement(arr: Array[Int],
i: Int, j: Int): Unit = {
// Get i location element
var temp: Int = arr(i);
// Swap value
arr(i) = arr(j);
arr(j) = temp;
}
// Function which is display array elements
def display(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
// iterating the array elements
while (i < size)
{
print("  " + arr(i));
i += 1;
}
print("\n");
}
// Function which is separating given array
// elements of Even and odd numbers
def segregateElement(arr: Array[Int], size: Int): Unit = {
// Just before the modifying array elements
print("Before Segregating Even and Odd elements \n");
// Display given array
this.display(arr, size);
var j: Int = 0;
var i: Int = 0;
// iterating the array elements
while (i < size)
{
if (arr(i) % 2 == 0)
{
// Swap element values
this.swapElement(arr, j, i);
// Visit to next location.
// Incremented location value by one
j += 1;
}
i += 1;
}
// After modify array elements
print("After Segregating Even and Odd elements \n");
this.display(arr, size);
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Segregation = new Segregation();
// Define arrays of integer elements
var arr1: Array[Int] = Array(1, 8, 7, 3, 9, 4, 2, 5, 10);
var arr2: Array[Int] = Array(9, 8, 2, 7, 6, 2, 4, 9, 7, 4);
// Get the number of elements in arr1
var size: Int = arr1.length;
// Check A
// Get the number of elements in arr2
size = arr2.length;
// Check B
}
}``````

#### Output

``````Before Segregating Even and Odd elements
1  8  7  3  9  4  2  5  10
After Segregating Even and Odd elements
8  4  2  10  9  1  7  5  3

Before Segregating Even and Odd elements
9  8  2  7  6  2  4  9  7  4
After Segregating Even and Odd elements
8  2  6  2  4  4  9  9  7  7
``````
``````import Foundation;
/*
Swift 4 Program
Segregate even and odd numbers in array
*/
class Segregation
{
// Swap array element
func swapElement(_ arr: inout[Int], _ i: Int, _ j: Int)
{
// Get i location element
let temp: Int = arr[i];
// Swap value
arr[i] = arr[j];
arr[j] = temp;
}
// Function which is display array elements
func display(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
// iterating the array elements
while (i < size)
{
print("  ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
// Function which is separating given array
// elements of Even and odd numbers
func segregateElement(_ arr: inout[Int], _ size: Int)
{
// Just before the modifying array elements
print("Before Segregating Even and Odd elements ");
// Display given array
self.display(arr, size);
var j: Int = 0;
var i: Int = 0;
// iterating the array elements
while (i < size)
{
if (arr[i] % 2 == 0)
{
// Swap element values
self.swapElement(&arr, j, i);
// Visit to next location.
// Incremented location value by one
j += 1;
}
i += 1;
}
// After modify array elements
print("After Segregating Even and Odd elements ");
self.display(arr, size);
print(terminator: "\n");
}
}
func main()
{
// Define arrays of integer elements
var arr1: [Int] = [1, 8, 7, 3, 9, 4, 2, 5, 10];
var arr2: [Int] = [9, 8, 2, 7, 6, 2, 4, 9, 7, 4];
// Get the number of elements in arr1
var size: Int = arr1.count;
// Check A
// Get the number of elements in arr2
size = arr2.count;
// Check B
}
main();``````

#### Output

``````Before Segregating Even and Odd elements
1   8   7   3   9   4   2   5   10
After Segregating Even and Odd elements
8   4   2   10   9   1   7   5   3

Before Segregating Even and Odd elements
9   8   2   7   6   2   4   9   7   4
After Segregating Even and Odd elements
8   2   6   2   4   4   9   9   7   7
``````
``````/*
Kotlin Program
Segregate even and odd numbers in array
*/
class Segregation
{
// Swap array element
fun swapElement(arr: Array < Int > , i: Int, j: Int): Unit
{
// Get i location element
val temp: Int = arr[i];
// Swap value
arr[i] = arr[j];
arr[j] = temp;
}
// Function which is display array elements
fun display(arr: Array < Int > , size: Int): Unit
{
var i: Int = 0;
// iterating the array elements
while (i < size)
{
print("  " + arr[i]);
i += 1;
}
print("\n");
}
// Function which is separating given array
// elements of Even and odd numbers
fun segregateElement(arr: Array < Int > , size: Int): Unit
{
// Just before the modifying array elements
print("Before Segregating Even and Odd elements \n");
// Display given array
this.display(arr, size);
var j: Int = 0;
var i: Int = 0;
// iterating the array elements
while (i < size)
{
if (arr[i] % 2 == 0)
{
// Swap element values
this.swapElement(arr, j, i);
// Visit to next location.
// Incremented location value by one
j += 1;
}
i += 1;
}
// After modify array elements
print("After Segregating Even and Odd elements \n");
this.display(arr, size);
print("\n");
}
}
fun main(args: Array < String > ): Unit
{
// Define arrays of integer elements
var arr1: Array < Int > = arrayOf(1, 8, 7, 3, 9, 4, 2, 5, 10);
var arr2: Array < Int > = arrayOf(9, 8, 2, 7, 6, 2, 4, 9, 7, 4);
// Get the number of elements in arr1
var size: Int = arr1.count();
// Check A
// Get the number of elements in arr2
size = arr2.count();
// Check B
}``````

#### Output

``````Before Segregating Even and Odd elements
1  8  7  3  9  4  2  5  10
After Segregating Even and Odd elements
8  4  2  10  9  1  7  5  3

Before Segregating Even and Odd elements
9  8  2  7  6  2  4  9  7  4
After Segregating Even and Odd elements
8  2  6  2  4  4  9  9  7  7
``````

Time complexity of this algorithm is O(n). Similar way your can move all odd elements at beginning and all even elements at the end of array.

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.