使用自定义比较器对数组中的奇数和偶数进行排序和分离
给定一个包含N个元素的数组arr[] ,任务是使用自定义比较器对数组中的奇数和偶数进行排序和分离。
例子:
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
方法:我们知道 std::sort() 用于按升序排序,但我们可以使用自定义比较器操作 sort() 以进行某些特定排序。
现在,为了将它们分开,可以使用的属性是偶数的最后一位为 0,奇数为 1。因此,使用自定义比较器根据最后一位对元素进行排序数字。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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() {
@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
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
输出
2 4 6 12 7 9 13 15
时间复杂度: O(N*log N)
辅助空间: O(1)