Sort and separate odd and even numbers in an Array using custom comparator
Last Updated :
13 Feb, 2023
Given an array arr[], containing N elements, the task is to sort and separate odd and even numbers in an Array using a custom comparator.
Example:
Input: arr[] = { 5, 3, 2, 8, 7, 4, 6, 9, 1 }
Output: 2 4 6 8 1 3 5 7 9
Input: arr[] = { 12, 15, 6, 2, 7, 13, 9, 4 }
Output: 2 4 6 12 7 9 13 15
Approach: As we know std::sort() is used for sorting in increasing order but we can manipulate sort() using a custom comparator to some specific sorting.
Now, to separate them, the property that can be used is that the last bit of an even number is 0 and in an odd number, it is 1. So, make the custom comparator to sort the elements based on the last bit of that number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool compare( int a, int b)
{
if ((a & 1) == (b & 1)) {
return a < b;
}
return (a & 1) < (b & 1);
}
void separateOddEven( int * arr, int N)
{
sort(arr, arr + N, compare);
for ( int i = 0; i < N; ++i) {
cout << arr[i] << ' ' ;
}
}
int main()
{
int arr[] = { 12, 15, 6, 2, 7, 13, 9, 4 };
int N = sizeof (arr) / sizeof ( int );
separateOddEven(arr, N);
}
|
Java
import java.util.*;
class GFG{
static boolean comparecust(Integer a, Integer b)
{
if ((a & 1 ) == (b & 1 )) {
return a < b;
}
return (a & 1 ) < (b & 1 );
}
static void separateOddEven(Integer []arr, int N)
{
Arrays.sort(arr, new Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b) {
if ((a & 1 ) == (b & 1 )) {
return a < b?- 1 : 1 ;
}
return ((a & 1 ) < (b & 1 ))?- 1 : 1 ;
}
});
for ( int i = 0 ; i < N; ++i) {
System.out.print(arr[i] + " " );
}
}
public static void main(String[] args)
{
Integer arr[] = { 12 , 15 , 6 , 2 , 7 , 13 , 9 , 4 };
int N = arr.length;
separateOddEven(arr, N);
}
}
|
Python3
from functools import cmp_to_key
def compare_cust(a, b):
if (a & 1 ) = = (b & 1 ):
return a - b
return (a & 1 ) - (b & 1 )
def separate_odd_even(arr):
arr.sort(key = cmp_to_key(compare_cust))
for i in arr:
print (i, end = " " )
if __name__ = = '__main__' :
arr = [ 12 , 15 , 6 , 2 , 7 , 13 , 9 , 4 ]
separate_odd_even(arr)
|
C#
using System;
using System.Collections;
class compare : IComparer
{
public int Compare(Object x, Object y)
{
int a = ( int )x;
int b = ( int )y;
if ((a & 1) == (b & 1))
{
return a < b ? -1 : 1;
}
return ((a & 1) < (b & 1)) ? -1 : 1;
}
}
class GFG{
static void separateOddEven( int []arr, int N)
{
IComparer cmp = new compare();
Array.Sort(arr, cmp);
for ( int i = 0; i < N; ++i)
{
Console.Write(arr[i] + " " );
}
}
public static void Main(String[] args)
{
int []arr = { 12, 15, 6, 2, 7, 13, 9, 4 };
int N = arr.Length;
separateOddEven(arr, N);
}
}
|
Javascript
<script>
function compare(a, b)
{
if ((a & 1) == (b & 1)) {
return a > b;
}
return (a & 1) > (b & 1);
}
function separateOddEven(arr, N)
{
arr.sort(compare);
for (let i = 0; i < N; ++i) {
document.write(arr[i] + " " );
}
}
let arr = [ 12, 15, 6, 2, 7, 13, 9, 4 ];
let N = arr.length;
separateOddEven(arr, N);
</script>
|
Output
2 4 6 12 7 9 13 15
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...