📌  相关文章
📜  分离偶数和奇数|套装3

📅  最后修改于: 2021-04-26 08:42:42             🧑  作者: Mango

给定一个整数数组,请在该数组中分隔偶数和奇数。所有的偶数应首先出现,然后是奇数。

例子:

我们在以下帖子中讨论了两种不同的方法:

  1. 分离偶数和奇数
  2. 分离偶数和奇数|套装2

蛮力解决方案

由于我们需要保持元素的顺序,因此可以通过以下步骤完成:

  1. 创建一个大小为n的临时数组A和一个整数ind,该数组将保留插入的元素的索引。
  2. 用零初始化ind并遍历原始数组,如果找到了偶数,则将该数字放在A [ind]处,然后递增ind的值。
  3. 再次遍历数组,如果找到奇数,则将其放入A [ind],然后递增ind的值。
  4. 遍历临时数组A并将其值复制到原始数组中。
C++
// C++ Implementation of the above approach
#include 
using namespace std;
void arrayEvenAndOdd(int arr[], int n)
{
    int a[n], ind = 0;
    for (int i = 0; i < n; i++)
    {
        if (arr[i] % 2 == 0)
        {
            a[ind] = arr[i];
            ind++;
        }
    }
    for (int i = 0; i < n; i++)
    {
        if (arr[i] % 2 != 0)
        {
            a[ind] = arr[i];
            ind++;
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 3, 2, 4, 7, 6, 9, 10 };
    int n = sizeof(arr) / sizeof(int);
 
    // Function call
    arrayEvenAndOdd(arr, n);
    return 0;
}


Python3
# Python3 implementation of the above approach
def arrayEvenAndOdd(arr,  n):
     
    ind = 0;
    a = [0 for i in range(n)]
     
    for i in range(n):
        if (arr[i] % 2 == 0):
            a[ind] = arr[i]
            ind += 1
 
    for i in range(n):
        if (arr[i] % 2 != 0):
            a[ind] = arr[i]
            ind += 1
 
    for i in range(n):
        print(a[i], end = " ")
         
    print()
 
# Driver code
arr = [ 1, 3, 2, 4, 7, 6, 9, 10 ]
n = len(arr)
 
# Function call
arrayEvenAndOdd(arr, n)
 
# This code is contributed by rohitsingh07052


Javascript


C++
// CPP code to segregate even odd
// numbers in an array
#include 
using namespace std;
 
// Function to segregate even odd numbers
void arrayEvenAndOdd(int arr[], int n)
{
 
    int i = -1, j = 0;
    int t;
    while (j != n) {
        if (arr[j] % 2 == 0) {
            i++;
 
            // Swapping even and odd numbers
            swap(arr[i], arr[j]);
        }
        j++;
    }
 
    // Printing segregated array
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Driver code
int main()
{
    int arr[] = { 1, 3, 2, 4, 7, 6, 9, 10 };
    int n = sizeof(arr) / sizeof(int);
    arrayEvenAndOdd(arr, n);
    return 0;
}


Java
// java code to segregate even odd
// numbers in an array
public class GFG {
      
    // Function to segregate even
    // odd numbers
    static void arrayEvenAndOdd(
                  int arr[], int n)
    {
      
        int i = -1, j = 0;
        while (j != n) {
            if (arr[j] % 2 == 0)
            {
                i++;
      
                // Swapping even and
                // odd numbers
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
            j++;
        }
      
        // Printing segregated array
        for (int k = 0; k < n; k++)
             System.out.print(arr[k] + " ");
    }
    
    // Driver code
    public static void main(String args[])
    {
        int arr[] = { 1, 3, 2, 4, 7,
                            6, 9, 10 };
        int n = arr.length;
        arrayEvenAndOdd(arr, n);
    }
}
 
// This code is contributed by Sam007


Python3
# Python3 code to segregate even odd
# numbers in an array
 
# Function to segregate even odd numbers
def arrayEvenAndOdd(arr,n):
    i = -1
    j= 0
    while (j!=n):
        if (arr[j] % 2 ==0):
            i = i+1
 
            # Swapping even and odd numbers
            arr[i],arr[j] = arr[j],arr[i]
             
        j = j+1
     
    # Printing segregated array
    for i in arr:
        print (str(i) + " " ,end='')
         
# Driver Code
if __name__=='__main__':
    arr = [ 1 ,3, 2, 4, 7, 6, 9, 10]
    n = len(arr)
    arrayEvenAndOdd(arr,n)
 
# This code was contributed by
# Yatin Gupta


C#
// C# code to segregate even odd
// numbers in an array
using System;
 
class GFG {
     
    // Function to segregate even
    // odd numbers
    static void arrayEvenAndOdd(
                  int []arr, int n)
    {
     
        int i = -1, j = 0;
        while (j != n) {
            if (arr[j] % 2 == 0)
            {
                i++;
     
                // Swapping even and
                // odd numbers
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
            j++;
        }
     
        // Printing segregated array
        for (int k = 0; k < n; k++)
            Console.Write(arr[k] + " ");
    }
 
    // Driver code   
    static void Main()
    {
        int []arr = { 1, 3, 2, 4, 7,
                            6, 9, 10 };
        int n = arr.Length;
        arrayEvenAndOdd(arr, n);
    }
}
 
// This code is contributed by Sam007


PHP


Javascript


输出
2 4 6 10 1 3 7 9 

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

高效方法:

以上方法的优化基于Lomuto的分区方案

  1. 保持指向数组中第一个奇数元素之前位置的指针。
  2. 遍历数组,如果遇到偶数,则将其与第一个奇数元素交换。
  3. 继续遍历。

下面是上述方法的实现:

C++

// CPP code to segregate even odd
// numbers in an array
#include 
using namespace std;
 
// Function to segregate even odd numbers
void arrayEvenAndOdd(int arr[], int n)
{
 
    int i = -1, j = 0;
    int t;
    while (j != n) {
        if (arr[j] % 2 == 0) {
            i++;
 
            // Swapping even and odd numbers
            swap(arr[i], arr[j]);
        }
        j++;
    }
 
    // Printing segregated array
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Driver code
int main()
{
    int arr[] = { 1, 3, 2, 4, 7, 6, 9, 10 };
    int n = sizeof(arr) / sizeof(int);
    arrayEvenAndOdd(arr, n);
    return 0;
}

Java

// java code to segregate even odd
// numbers in an array
public class GFG {
      
    // Function to segregate even
    // odd numbers
    static void arrayEvenAndOdd(
                  int arr[], int n)
    {
      
        int i = -1, j = 0;
        while (j != n) {
            if (arr[j] % 2 == 0)
            {
                i++;
      
                // Swapping even and
                // odd numbers
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
            j++;
        }
      
        // Printing segregated array
        for (int k = 0; k < n; k++)
             System.out.print(arr[k] + " ");
    }
    
    // Driver code
    public static void main(String args[])
    {
        int arr[] = { 1, 3, 2, 4, 7,
                            6, 9, 10 };
        int n = arr.length;
        arrayEvenAndOdd(arr, n);
    }
}
 
// This code is contributed by Sam007

Python3

# Python3 code to segregate even odd
# numbers in an array
 
# Function to segregate even odd numbers
def arrayEvenAndOdd(arr,n):
    i = -1
    j= 0
    while (j!=n):
        if (arr[j] % 2 ==0):
            i = i+1
 
            # Swapping even and odd numbers
            arr[i],arr[j] = arr[j],arr[i]
             
        j = j+1
     
    # Printing segregated array
    for i in arr:
        print (str(i) + " " ,end='')
         
# Driver Code
if __name__=='__main__':
    arr = [ 1 ,3, 2, 4, 7, 6, 9, 10]
    n = len(arr)
    arrayEvenAndOdd(arr,n)
 
# This code was contributed by
# Yatin Gupta

C#

// C# code to segregate even odd
// numbers in an array
using System;
 
class GFG {
     
    // Function to segregate even
    // odd numbers
    static void arrayEvenAndOdd(
                  int []arr, int n)
    {
     
        int i = -1, j = 0;
        while (j != n) {
            if (arr[j] % 2 == 0)
            {
                i++;
     
                // Swapping even and
                // odd numbers
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
            j++;
        }
     
        // Printing segregated array
        for (int k = 0; k < n; k++)
            Console.Write(arr[k] + " ");
    }
 
    // Driver code   
    static void Main()
    {
        int []arr = { 1, 3, 2, 4, 7,
                            6, 9, 10 };
        int n = arr.Length;
        arrayEvenAndOdd(arr, n);
    }
}
 
// This code is contributed by Sam007

的PHP


Java脚本


输出
2 4 6 10 7 1 9 3 

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