Sort and separate odd and even numbers in an Array using custom comparator - GeeksforGeeks

12 Jan, 2022
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++
Java
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Creating custom comparatorbool compare(int a, int b){     // If both are odd or even    // then sorting in increasing order    if ((a & 1) == (b & 1)) {        return a < b;    }     // Sorting on the basis of last bit if    // if one is odd and the other one is even    return (a & 1) < (b & 1);} // Function tovoid separateOddEven(int* arr, int N){    // Separating them using sort comparator    sort(arr, arr + N, compare);     for (int i = 0; i < N; ++i) {        cout << arr[i] << ' ';    }} // Driver Codeint main(){    int arr[] = { 12, 15, 6, 2, 7, 13, 9, 4 };    int N = sizeof(arr) / sizeof(int);    separateOddEven(arr, N);}
// Java program for the above approachimport java.util.*; class GFG{ // Creating custom comparatorstatic boolean comparecust(Integer a, Integer b){     // If both are odd or even    // then sorting in increasing order    if ((a & 1) == (b & 1)) {        return a < b;    }     // Sorting on the basis of last bit if    // if one is odd and the other one is even    return (a & 1) < (b & 1);} // Function tostatic void separateOddEven(Integer []arr, int N){    // Separating them using sort comparator    Arrays.sort(arr, new Comparator<Integer>() {         @Override        public int compare(Integer a, Integer b) {            // If both are odd or even            // then sorting in increasing order            if ((a & 1) == (b & 1)) {                return a < b?-1:1;            }             // Sorting on the basis of last bit if            // if one is odd and the other one is even            return ((a & 1) < (b & 1))?-1:1;        }             });     for (int i = 0; i < N; ++i) {        System.out.print(arr[i] +" ");    }} // Driver Codepublic static void main(String[] args){    Integer arr[] = { 12, 15, 6, 2, 7, 13, 9, 4 };    int N = arr.length;    separateOddEven(arr, N);}} // This code is contributed by 29AjayKumar
// C# program for the above approachusing System;using System.Collections; class compare : IComparer{         // Call CaseInsensitiveComparer.Compare    public int Compare(Object x, Object y)    {        int a = (int)x;        int b = (int)y;                 // If both are odd or even        // then sorting in increasing order        if ((a & 1) == (b & 1))        {            return a < b ? -1 : 1;        }         // Sorting on the basis of last bit if        // if one is odd and the other one is even        return ((a & 1) < (b & 1)) ? -1 : 1;    }} class GFG{ // Function tostatic void separateOddEven(int []arr, int N){         // Separating them using sort comparator    // Instantiate the IComparer object    IComparer cmp = new compare();    Array.Sort(arr, cmp);     for(int i = 0; i < N; ++i)    {        Console.Write(arr[i] + " ");    }} // Driver Codepublic static void Main(String[] args){    int []arr = { 12, 15, 6, 2, 7, 13, 9, 4 };    int N = arr.Length;         separateOddEven(arr, N);}} // This code is contributed by shikhasingrajput
<script>// Javascript program for the above approach // Creating custom comparatorfunction compare(a, b){     // If both are odd or even    // then sorting in increasing order    if ((a & 1) == (b & 1)) {        return a > b;    }     // Sorting on the basis of last bit if    // if one is odd and the other one is even    return (a & 1) > (b & 1);} // Function tofunction separateOddEven(arr, N){    // Separating them using sort comparator    arr.sort(compare);     for (let i = 0; i < N; ++i) {        document.write(arr[i] + " ");    }} // Driver Codelet arr = [ 12, 15, 6, 2, 7, 13, 9, 4 ];let N = arr.length;separateOddEven(arr, N); // This code is contributed by Samim Hossain Mondal.</script>
2 4 6 12 7 9 13 15 
Time Complexity: O(N*log N)Auxiliary Space: O(1)
 
samim2000
29AjayKumar
shikhasingrajput
sagartomar9927
C++
Sorting
Sorting
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Sorting a vector in C++
Friend class and function in C++
Pair in C++ Standard Template Library (STL)