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 9Input: 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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Creating custom comparator bool 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 to void 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 Code int main() { int arr[] = { 12, 15, 6, 2, 7, 13, 9, 4 }; int N = sizeof (arr) / sizeof ( int ); separateOddEven(arr, N); } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Creating custom comparator static 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 to static 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 Code public 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 |
Python3
# Python code from functools import cmp_to_key def compare_cust(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 one is odd and the other one is even return (a & 1 ) - (b & 1 ) def separate_odd_even(arr): # Separating them using sort comparator arr.sort(key = cmp_to_key(compare_cust)) for i in arr: print (i, end = " " ) # Driver code if __name__ = = '__main__' : arr = [ 12 , 15 , 6 , 2 , 7 , 13 , 9 , 4 ] separate_odd_even(arr) # This code is contributed by Edula Vinay Kumar Reddy |
C#
// C# program for the above approach using 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 to static 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 Code public 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 |
Javascript
<script> // Javascript program for the above approach // Creating custom comparator function 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 to function separateOddEven(arr, N) { // Separating them using sort comparator arr.sort(compare); for (let i = 0; i < N; ++i) { document.write(arr[i] + " " ); } } // Driver Code let 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)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!