📜  以升序交替排列奇数和偶数值

📅  最后修改于: 2021-05-06 19:56:14             🧑  作者: Mango

给定一个整数数组(奇数和偶数),任务是按照使奇数和偶数分别以非降序(升序)交替出现的方式进行排列。

  • 如果最小值为偶数,则必须打印偶数-奇数图案。
  • 如果最小值是奇数,那么我们必须打印奇偶图案。

注意:输入数组中的奇数元素数量必须等于偶数元素的数量。

例子:

要求: Microsoft Tech-Set-Go-2018

算法:

  1. 对给定的数组进行排序。
  2. 在“ List-1”中插入“偶数”值,在“ List-2”中插入“奇数”值。
  3. 现在,如果最小值是偶数,则将列表1中的偶数和列表2中的奇数插入原始数组,依此类推。
  4. 但是,如果最小值是奇数,则将列表2中的奇数值和列表1中的偶数值插入原始数组,依此类推。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
  
void AlternateRearrange(int arr[], int n)
{
    // Sort the array
    sort(arr, arr + n);
  
    vector v1; // to insert even values
    vector v2; // to insert odd values
  
    for (int i = 0; i < n; i++)
        if (arr[i] % 2 == 0)
            v1.push_back(arr[i]);
        else
            v2.push_back(arr[i]);
  
    int index = 0, i = 0, j = 0;
  
    bool flag = false;
  
    // Set flag to true if first element is even
    if (arr[0] % 2 == 0)
        flag = true;
  
    // Start rearranging array
    while (index < n) {
  
        // If first element is even
        if (flag == true) {
            arr[index++] = v1[i++];
            flag = !flag;
        }
  
        // Else, first element is Odd
        else {
            arr[index++] = v2[j++];
            flag = !flag;
        }
    }
  
    // Print the rearranged array
    for (i = 0; i < n; i++)
        cout << arr[i] << " ";
}
  
// Driver code
int main()
{
    int arr[] = { 9, 8, 13, 2, 19, 14 };
    int n = sizeof(arr) / sizeof(int);
    AlternateRearrange(arr, n);
    return 0;
}


Java
// Java implementation of the above approach 
  
import java.util.* ;
  
class GFG
{
  
    static void AlternateRearrange(int arr[], int n) 
    { 
        // Sort the array
          
        // Collection.sort() sorts the
        // collection in ascending order
        Arrays.sort(arr) ;
          
        Vector v1 = new Vector(); // to insert even values 
        Vector v2 = new Vector(); // to insert odd values 
      
        for (int i = 0; i < n; i++) 
            if (arr[i] % 2 == 0) 
                v1.add(arr[i]); 
            else
                v2.add(arr[i]); 
      
        int index = 0, i = 0, j = 0; 
      
        boolean flag = false; 
      
        // Set flag to true if first element is even 
        if (arr[0] % 2 == 0) 
            flag = true; 
      
        // Start rearranging array 
        while (index < n) 
        { 
      
            // If first element is even 
            if (flag == true) 
            { 
                arr[index] = (int)v1.get(i); 
                i += 1 ;
                index += 1 ;
                flag = !flag; 
            } 
      
            // Else, first element is Odd 
            else
            { 
                arr[index] = (int)v2.get(j) ; 
                j += 1 ;
                index += 1 ;
                flag = !flag; 
            } 
        } 
      
        // Print the rearranged array 
        for (i = 0; i < n; i++) 
            System.out.print(arr[i] + " "); 
    } 
      
    // Driver code 
    public static void main(String []args) 
    { 
        int arr[] = { 9, 8, 13, 2, 19, 14 }; 
        int n = arr.length ;
      
        AlternateRearrange(arr, n); 
    } 
}
  
// This code is contributed by aishwarya.27


Python3
# Python3 implementation of the above approach
  
def AlternateRearrange(arr, n):
  
    # Sort the array
    arr.sort()
  
    v1 = list() # to insert even values
    v2 = list() # to insert odd values
  
    for i in range(n):
        if (arr[i] % 2 == 0):
            v1.append(arr[i])
        else:
            v2.append(arr[i])
  
    index = 0
    i = 0
    j = 0
  
    flag = False
  
    # Set flag to true if first element is even
    if (arr[0] % 2 == 0):
        flag = True
  
    # Start rearranging array
    while (index < n): 
  
        # If first element is even
        if (flag == True):
            arr[index] = v1[i]
            index += 1
            i += 1
            flag = ~flag
          
        # Else, first element is Odd
        else:
            arr[index] = v2[j]
            index += 1
            j += 1
  
            flag = ~flag
          
    # Print the rearranged array
    for i in range(n):
        print(arr[i], end = " ")
  
# Driver code
arr = [ 9, 8, 13, 2, 19, 14] 
n = len(arr)
  
AlternateRearrange(arr, n)
  
# This code is contributed 
# by Mohit Kumar 29


C#
// C# implementation of the above approach 
  
using System;
using System.Collections;
class GFG
{
  
    static void AlternateRearrange(int []arr, int n) 
    { 
        // Sort the array
          
        // Collection.sort() sorts the
        // collection in ascending order
        Array.Sort(arr) ;
          
        ArrayList v1 = new ArrayList(); // to insert even values 
        ArrayList v2 = new ArrayList(); // to insert odd values 
      
        for (int j = 0; j < n; j++) 
            if (arr[j] % 2 == 0) 
                v1.Add(arr[j]); 
            else
                v2.Add(arr[j]); 
      
        int index = 0, i = 0, k = 0; 
      
        bool flag = false; 
      
        // Set flag to true if first element is even 
        if (arr[0] % 2 == 0) 
            flag = true; 
      
        // Start rearranging array 
        while (index < n) 
        { 
      
            // If first element is even 
            if (flag == true) 
            { 
                arr[index] = (int)v1[i]; 
                i += 1 ;
                index += 1 ;
                flag = !flag; 
            } 
      
            // Else, first element is Odd 
            else
            { 
                arr[index] = (int)v2[k] ; 
                k += 1 ;
                index += 1 ;
                flag = !flag; 
            } 
        } 
      
        // Print the rearranged array 
        for (i = 0; i < n; i++) 
            Console.Write(arr[i] + " "); 
    } 
      
    // Driver code 
    static void Main() 
    { 
        int []arr = { 9, 8, 13, 2, 19, 14 }; 
        int n = arr.Length ;
      
        AlternateRearrange(arr, n); 
    } 
}
  
// This code is contributed by mits


PHP


输出:
2 9 8 13 14 19