Open In App

Segregate even and odd numbers using Lomuto’s Partition Scheme

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[] = {7, 2, 9, 4, 6, 1, 3, 8, 5}
Output: 2 4 6 8 7 9 1 3 5

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: 

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

For the sake of illustration, let’s consider the following array: [7, 2, 9, 4, 6, 1, 3, 8, 5].

In this case, let’s choose the last element, which is 5, as the pivot.

Let’s go through the steps:

  • Initially, i = 0 and j = 0.
  • The first element is 7 (odd),
    • so we do nothing and move j to the next element.
  • The second element is 2 (even),
    • so we swap it with the element at index i (which is also 2) and increment i to 1.
  • The third element is 9 (odd),
    • so we do nothing and move j to the next element.
  • The fourth element is 4 (even),
    • so we swap it with the element at index i (which is 9) and increment i to 2.
  • The fifth element is 6 (even),
    • so we swap it with the element at index i (which is 9) and increment i to 3.
  • The sixth element is 1 (odd),
    • so we do nothing and move j to the next element.
  • The seventh element is 3 (odd),
    • so we do nothing and move j to the next element.
  • The eighth element is 8 (even),
    • so we swap it with the element at index i (which is 1) and increment i to 4.
  • The ninth element is 5 (odd),
    • so we do nothing and move j to the next element.

The array now looks like this: [2, 4, 6, 8, 5, 1, 3, 9, 7].

In this case, we swap 5 with 7. This step ensures that the pivot element is in its final sorted position.

The array after the final swap will be: [2, 4, 6, 8, 7, 1, 3, 9, 5].

Below is the implementation of the above approach: 

// CPP code to segregate even odd
// numbers in an array
#include <bits/stdc++.h>
using namespace std;

// Function to segregate even odd numbers
vector<int> arrayEvenAndOdd(vector<int>& arr)
{

    int i = -1, j = 0;
    int pivot = arr.back();
    for (j = 0; j < arr.size() - 1; j++) {
        if (arr[j] % 2 == 0) {
            i++;

            // Swapping even and odd numbers
            swap(arr[i], arr[j]);
        }
    }

    swap(arr[i + 1], arr.back());
    return arr;
}

// Driver code
int main()
{
    vector<int> arr = { 7, 2, 9, 4, 6, 1, 3, 8, 5 };
    vector<int> updatedArr = arrayEvenAndOdd(arr);

    // Function Call
    cout << "Updated Array: ";
    for (int num : updatedArr) {
        cout << num << " ";
    }
    cout << endl;

    return 0;
}
// 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 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# 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
<script>
// JavaScript code to segregate even odd
// numbers in an array

// Function to segregate even odd numbers
function arrayEvenAndOdd(arr, n)
{

    let i = -1, j = 0;
    let t;
    while (j != n) {
        if (arr[j] % 2 == 0) {
            i++;

            // Swapping even and odd numbers
                let temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
        }
        j++;
    }

    // Printing segregated array
    for (let i = 0; i < n; i++)
        document.write(arr[i] + " ");
}

// Driver code

    let arr = [ 1, 3, 2, 4, 7, 6, 9, 10 ];
    let n = arr.length;
    arrayEvenAndOdd(arr, n);

// This code is contributed by Surbhi Tyagi

</script>
<?php
// PHP code to segregate even odd
// numbers in an array

// Function to segregate
// even odd numbers
function arrayEvenAndOdd($arr, $n)
{
    $i = -1;
    $j = 0;
    $t;
    while ($j != $n)
    {
        if ($arr[$j] % 2 == 0)
        {
            $i++;

            // Swapping even and
            // odd numbers
            $x = $arr[$i];
            $arr[$i] = $arr[$j];
            $arr[$j] = $x;
        }
        $j++;
    }

    // Printing segregated 
    // array
    for ($i = 0; $i < $n; $i++)
        echo $arr[$i] . " ";
}

    // Driver code
    $arr = array(1, 3, 2, 4, 7, 6, 9, 10);
    $n = sizeof($arr);
    arrayEvenAndOdd($arr, $n);

// This code is contributed by Anuj_67
?>
Output
Updated Array: 2 4 6 8 5 1 3 7 9 

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


Article Tags :