📜  隔离偶数和奇数 | 2套

📅  最后修改于: 2021-09-07 02:17:30             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是分离偶数和奇数。先打印所有偶数,然后打印奇数。

例子:

线性方法:这个问题可以看作是荷兰国旗问题的变体。有关解决问题的所有线性方法,请参阅上一篇文章。

时间复杂度: O(N)
辅助空间: O(1)

使用stable_partition()函数: stable_partition()算法排列由startend定义的序列,使得用户定义的谓词函数指定的所有元素返回True ,在函数返回False 的元素之前。分区稳定。因此,保留了序列的相对顺序。

句法:

参数:

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to segregate
// odd and even numbers
void segregate(vector arr)
{
 
    // Using stable partition
    // with lambda expression
    stable_partition(arr.begin(),
                     arr.end(),
                     [](int a) {
                         return a % 2 == 0;
                     });
 
    // Print array after partition
    for (int num : arr)
        cout << num << " ";
}
 
// Driver Code
int main()
{
    // Given array arr[]
    vector arr = { 18, 52, 37, 70,
                        3, 63, 2, 34 };
 
    // Function Call
    segregate(arr);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
   
static int[] stable_partition(int arr[])
{
  // Initialize left and
  // right indexes
  int left = 0,
      right = arr.length - 1;
   
  while (left < right)
  {
    // Increment left index while
    // we see 0 at left
    while (arr[left] % 2 == 0 &&
           left < right)
      left++;
 
    // Decrement right index while
    // we see 1 at right
    while (arr[right] % 2 == 1 &&
           left < right)
      right--;
 
    if (left < right)
    {
      // Swap arr[left] and
      // arr[right]
      int temp = arr[left];
      arr[left] = arr[right];
      arr[right] = temp;
      left++;
      right--;
    }
  }
  return arr;
}
 
// Function to segregate
// odd and even numbers
static void segregate(int[] arr)
{
  // Using stable partition
  // with lambda expression
  int []ans = stable_partition(arr);
 
  // Print array after partition
  for (int num : ans)
    System.out.print(num + " ");
}
 
// Driver Code
public static void main(String[] args)
{
  // Given array arr[]
  int[] arr = {18, 52, 37, 70,
               3, 63, 2, 34};
 
  // Function Call
  segregate(arr);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to implement
# the above approach
 
# Function to segregate
# odd and even numbers
def segregate(arr):
     
    # Using stable partition
    # with lambda expression
    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 = " ")
 
# Driver Code
if __name__ == '__main__':
     
    # Given array arr[]
    arr = [ 18, 52, 37, 70, 3, 63, 2, 34 ]
 
    # Function Call
    segregate(arr)
 
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
   
static void stable_partition(int []arr)
{
  // Initialize left and
  // right indexes
  List odd = new List();
  List even = new List();
 
  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 +" ");
}
 
// Function to segregate
// odd and even numbers
static void segregate(int[] arr)
{
  // Using stable partition
  // with lambda expression
  stable_partition(arr);
 
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given array []arr
  int[] arr = {18, 52, 37, 70,
               3, 63, 2, 34};
 
  // Function Call
  segregate(arr);
}
}
 
// This code is contributed by 29AjayKumar


输出
18 52 70 2 34 37 3 63 

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live