GeeksforGeeks App
Open App
Browser
Continue

Segregate even and odd numbers | Set 3

Given an array arr[] of integers, segregate even and odd numbers in the array. Such that all the even numbers should be present first, and then the odd numbers.

Examples:

Input: arr[] = 1 9 5 3 2 6 7 11
Output: 2 6 5 3 1 9 7 11

Input: arr[] = 1 3 2 4 7 6 9 10
Output:  2 4 6 10 7 1 9 3

We have discussed two different approaches in below posts:

Brute-Force Solution:

As we need to maintain the order of elements then this can be done in the following steps :

1. Create a temporary array A of size n and an integer index which will keep the index of elements inserted .
2. Initialize index with zero and iterate over the original array and if even number is found then put that number at A[index] and then increment the value of index .
3. Again iterate over array and if an odd number is found then put it in A[index] and then increment the value of index.
4. Iterate over the temporary array A and copy its values in the original array.

C++

 `// C++ Implementation of the above approach``#include ``using` `namespace` `std;``void` `arrayEvenAndOdd(``int` `arr[], ``int` `n)``{``    ``int` `a[n], index = 0;``    ``for` `(``int` `i = 0; i < n; i++)``    ``{``        ``if` `(arr[i] % 2 == 0)``        ``{``            ``a[index] = arr[i];``            ``index++;``        ``}``    ``}``    ``for` `(``int` `i = 0; i < n; i++)``    ``{``        ``if` `(arr[i] % 2 != 0)``        ``{``            ``a[index] = arr[i];``            ``index++;``        ``}``    ``}``    ``for` `(``int` `i = 0; i < n; i++)``    ``{``        ``cout << a[i] << ``" "``;``    ``}``    ``cout << endl;``}` `// Driver code``int` `main()``{``    ``int` `arr[] = { 1, 3, 2, 4, 7, 6, 9, 10 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(``int``);` `    ``// Function call``    ``arrayEvenAndOdd(arr, n);``    ``return` `0;``}`

Java

 `// Java Implementation of the above approach``import` `java.io.*;``class` `GFG``{``public` `static` `void` `arrayEvenAndOdd(``int` `arr[], ``int` `n)``{``    ``int``[] a;``    ``a = ``new` `int``[n];``    ``int` `index = ``0``;``    ``for` `(``int` `i = ``0``; i < n; i++)``    ``{``        ``if` `(arr[i] % ``2` `== ``0``)``        ``{``            ``a[index] = arr[i];``            ``index++;``        ``}``    ``}``    ``for` `(``int` `i = ``0``; i < n; i++)``    ``{``        ``if` `(arr[i] % ``2` `!= ``0``)``        ``{``            ``a[index] = arr[i];``            ``index++;``        ``}``    ``}``    ``for` `(``int` `i = ``0``; i < n; i++)``    ``{``        ``System.out.print(a[i] + ``" "``);``    ``}``    ``System.out.println(``""``);``}``    ``// Driver code``    ``public` `static` `void` `main (String[] args)``    ``{``      ` `      ``int` `arr[] = { ``1``, ``3``, ``2``, ``4``, ``7``, ``6``, ``9``, ``10` `};``            ``int` `n = arr.length;` `            ``// Function call``            ``arrayEvenAndOdd(arr, n);``    ``}``}` `// This code is contributed by rohitsingh07052.`

Python3

 `# Python3 implementation of the above approach``def` `arrayEvenAndOdd(arr,  n):``    ` `    ``index ``=` `0``;``    ``a ``=` `[``0` `for` `i ``in` `range``(n)]``    ` `    ``for` `i ``in` `range``(n):``        ``if` `(arr[i] ``%` `2` `=``=` `0``):``            ``a[index] ``=` `arr[i]``            ``ind ``+``=` `1` `    ``for` `i ``in` `range``(n):``        ``if` `(arr[i] ``%` `2` `!``=` `0``):``            ``a[index] ``=` `arr[i]``            ``ind ``+``=` `1` `    ``for` `i ``in` `range``(n):``        ``print``(a[i], end ``=` `" "``)``        ` `    ``print``()` `# Driver code``arr ``=` `[ ``1``, ``3``, ``2``, ``4``, ``7``, ``6``, ``9``, ``10` `]``n ``=` `len``(arr)` `# Function call``arrayEvenAndOdd(arr, n)` `# This code is contributed by rohitsingh07052`

C#

 `// C# implementation of the above approach``using` `System;` `class` `GFG{``    ` `static` `void` `arrayEvenAndOdd(``int``[] arr, ``int` `n)``{``    ``int``[] a = ``new` `int``[n];``    ``int` `index = 0;``    ` `    ``for``(``int` `i = 0; i < n; i++)``    ``{``        ``if` `(arr[i] % 2 == 0)``        ``{``            ``a[index] = arr[i];``            ``index++;``        ``}``    ``}``    ``for``(``int` `i = 0; i < n; i++)``    ``{``        ``if` `(arr[i] % 2 != 0)``        ``{``            ``a[index] = arr[i];``            ``index++;``        ``}``    ``}``    ``for``(``int` `i = 0; i < n; i++)``    ``{``        ``Console.Write(a[i] + ``" "``);``    ``}``}` `// Driver code``static` `public` `void` `Main()``{``    ``int``[] arr = { 1, 3, 2, 4, 7, 6, 9, 10 };``    ``int` `n = arr.Length;``    ` `    ``arrayEvenAndOdd(arr, n);``}``}` `// This code is contributed by Potta Lokesh`

Javascript

 ``

Output

`2 4 6 10 1 3 7 9 `

Time complexity: O(n)
Auxiliary space: O(n)

Efficient Approach:

The optimization for above approach is based on Lomuto’s Partition Scheme

1. Maintain a pointer to the position before first odd element in the array.
2. Traverse the array and if even number is encountered then swap it with the first odd element.
3. Continue the traversal.

Below is the implementation of the above approach:

C++

 `// CPP code to segregate even odd``// numbers in an array``#include ``using` `namespace` `std;` `// Function to segregate even odd numbers``void` `arrayEvenAndOdd(``int` `arr[], ``int` `n)``{` `    ``int` `i = -1, j = 0;``    ``int` `t;``    ``while` `(j != n) {``        ``if` `(arr[j] % 2 == 0) {``            ``i++;` `            ``// Swapping even and odd numbers``            ``swap(arr[i], arr[j]);``        ``}``        ``j++;``    ``}` `    ``// Printing segregated array``    ``for` `(``int` `i = 0; i < n; i++)``        ``cout << arr[i] << ``" "``;``}` `// Driver code``int` `main()``{``    ``int` `arr[] = { 1, 3, 2, 4, 7, 6, 9, 10 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(``int``);``    ``arrayEvenAndOdd(arr, n);``    ``return` `0;``}`

Java

 `// java code to segregate even odd``// numbers in an array``public` `class` `GFG {``     ` `    ``// Function to segregate even``    ``// odd numbers``    ``static` `void` `arrayEvenAndOdd(``                  ``int` `arr[], ``int` `n)``    ``{``     ` `        ``int` `i = -``1``, j = ``0``;``        ``while` `(j != n) {``            ``if` `(arr[j] % ``2` `== ``0``)``            ``{``                ``i++;``     ` `                ``// Swapping even and``                ``// odd numbers``                ``int` `temp = arr[i];``                ``arr[i] = arr[j];``                ``arr[j] = temp;``            ``}``            ``j++;``        ``}``     ` `        ``// Printing segregated array``        ``for` `(``int` `k = ``0``; k < n; k++)``             ``System.out.print(arr[k] + ``" "``);``    ``}``   ` `    ``// Driver code``    ``public` `static` `void` `main(String args[])``    ``{``        ``int` `arr[] = { ``1``, ``3``, ``2``, ``4``, ``7``,``                            ``6``, ``9``, ``10` `};``        ``int` `n = arr.length;``        ``arrayEvenAndOdd(arr, n);``    ``}``}` `// This code is contributed by Sam007`

Python3

 `# Python3 code to segregate even odd``# numbers in an array` `# Function to segregate even odd numbers``def` `arrayEvenAndOdd(arr,n):``    ``i ``=` `-``1``    ``j``=` `0``    ``while` `(j!``=``n):``        ``if` `(arr[j] ``%` `2` `=``=``0``):``            ``i ``=` `i``+``1` `            ``# Swapping even and odd numbers``            ``arr[i],arr[j] ``=` `arr[j],arr[i]``            ` `        ``j ``=` `j``+``1``    ` `    ``# Printing segregated array``    ``for` `i ``in` `arr:``        ``print` `(``str``(i) ``+` `" "` `,end``=``'')``        ` `# Driver Code``if` `__name__``=``=``'__main__'``:``    ``arr ``=` `[ ``1` `,``3``, ``2``, ``4``, ``7``, ``6``, ``9``, ``10``]``    ``n ``=` `len``(arr)``    ``arrayEvenAndOdd(arr,n)` `# This code was contributed by``# Yatin Gupta`

C#

 `// C# code to segregate even odd``// numbers in an array``using` `System;` `class` `GFG {``    ` `    ``// Function to segregate even``    ``// odd numbers``    ``static` `void` `arrayEvenAndOdd(``                  ``int` `[]arr, ``int` `n)``    ``{``    ` `        ``int` `i = -1, j = 0;``        ``while` `(j != n) {``            ``if` `(arr[j] % 2 == 0)``            ``{``                ``i++;``    ` `                ``// Swapping even and``                ``// odd numbers``                ``int` `temp = arr[i];``                ``arr[i] = arr[j];``                ``arr[j] = temp;``            ``}``            ``j++;``        ``}``    ` `        ``// Printing segregated array``        ``for` `(``int` `k = 0; k < n; k++)``            ``Console.Write(arr[k] + ``" "``);``    ``}` `    ``// Driver code   ``    ``static` `void` `Main()``    ``{``        ``int` `[]arr = { 1, 3, 2, 4, 7,``                            ``6, 9, 10 };``        ``int` `n = arr.Length;``        ``arrayEvenAndOdd(arr, n);``    ``}``}` `// This code is contributed by Sam007`

PHP

 ``

Javascript

 ``

Output

`2 4 6 10 7 1 9 3 `

Time Complexity : O(n)
Auxiliary Space : O(1), since no extra space has been taken.

My Personal Notes arrow_drop_up