Segregate Even and Odd numbers | Set 2
Last Updated :
16 Aug, 2022
Given an array arr[] of size N, the task is to segregate even and odd numbers. Print all even numbers first, and then odd numbers.
Examples:
Input: arr[] = {8, 22, 65, 70, 33, 60, 2, 34, 43, 21}
Output: {8, 22, 70, 60, 2, 34, 65, 33, 43, 21}
Input: arr[] = {18, 52, 37, 70, 3, 63, 2, 34}
Output: {18, 52, 70, 2, 34, 37, 3, 63}
Linear Approach: This problem can be seen as a variation of the Dutch National Flag Problem. Refer to the previous post for all the linear approaches to solve the problem.
Time Complexity: O(N)
Auxiliary Space: O(1)
Approach using stable_partition() Function: The stable_partition() algorithm arranges the sequence defined by start and end such that all elements for which the predicate, specified by user-defined predicate function, returns True, precedes the ones for which the function returns False. The partitioning is stable. Therefore, the relative ordering of the sequence is preserved.
Syntax:
template
BiIter stable_partition(BiIter start, BiIter end, UnPred pfn)
Parameters:
start: the range of elements to reorder
end: the range of elements to reorder
pfn: User-defined predicate function object that defines the condition to be satisfied if an element is to be classified.
A predicate takes a single argument and returns True or False.
Return Value: Returns an iterator to the beginning of the elements for which the predicate is false.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void segregate(vector< int > arr)
{
stable_partition(arr.begin(),
arr.end(),
[]( int a) {
return a % 2 == 0;
});
for ( int num : arr)
cout << num << " " ;
}
int main()
{
vector< int > arr = { 18, 52, 37, 70,
3, 63, 2, 34 };
segregate(arr);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int [] stable_partition( int arr[])
{
int left = 0 ,
right = arr.length - 1 ;
while (left < right)
{
while (arr[left] % 2 == 0 &&
left < right)
left++;
while (arr[right] % 2 == 1 &&
left < right)
right--;
if (left < right)
{
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
return arr;
}
static void segregate( int [] arr)
{
int []ans = stable_partition(arr);
for ( int num : ans)
System.out.print(num + " " );
}
public static void main(String[] args)
{
int [] arr = { 18 , 52 , 37 , 70 ,
3 , 63 , 2 , 34 };
segregate(arr);
}
}
|
Python3
def segregate(arr):
odd = []
even = []
for x in arr:
if (x % 2 = = 0 ):
even.append(x)
else :
odd.append(x)
for x in even:
print (x, end = " " )
for x in odd:
print (x, end = " " )
if __name__ = = '__main__' :
arr = [ 18 , 52 , 37 , 70 , 3 , 63 , 2 , 34 ]
segregate(arr)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void stable_partition( int []arr)
{
List< int > odd = new List< int >();
List< int > even = new List< int >();
foreach ( int i in arr)
{
if (i % 2 == 1)
odd.Add(i);
else
even.Add(i);
}
foreach ( int i in even)
Console.Write(i + " " );
foreach ( int i in odd)
Console.Write(i + " " );
}
static void segregate( int [] arr)
{
stable_partition(arr);
}
public static void Main(String[] args)
{
int [] arr = {18, 52, 37, 70,
3, 63, 2, 34};
segregate(arr);
}
}
|
Javascript
<script>
function stable_partition( arr)
{
var left = 0,
right = arr.length - 1;
while (left < right)
{
while (arr[left] % 2 == 0 &&
left < right)
left++;
while (arr[right] % 2 == 1 &&
left < right)
right--;
if (left < right)
{
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
return arr;
}
var arr = [ 18, 52, 37, 70,3, 63, 2, 34 ];
segregateEvenOdd(arr);
for (i = 0; i < arr.length; i++)
document.write(arr[i]+ " " )
</script>
|
Output
18 52 70 2 34 37 3 63
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...