📌  相关文章
📜  按升序对偶数和奇数放置的元素进行排序

📅  最后修改于: 2021-04-26 18:27:26             🧑  作者: Mango

给定列表N ,任务是按升序对奇数和偶数位置的所有元素进行排序。排序后,我们需要将所有奇数位置的元素放在一起,然后将所有偶数位置的元素放在一起

例子:

Input : [3, 2, 7, 6, 8]
Output : 3 7 8 2 6
Explanation: 
Odd position elements in sorted order are 3, 7, 8.
Even position elements in sorted order are 2, 6.

Input : 1 0 2 7 0 0
Output : 0 1 2 0 0 7
Odd {1, 2, 0}
Even {0, 7, 0}

方法:

  • 初始化两个列表以存储奇数和偶数索引数字。
  • 遍历所有数字,并将奇数索引的数字存储在odd_indexes列表中,将偶数索引的数字存储在even_indexes列表中。
  • 按排序顺序打印odd_indexes列表中的元素。
  • 按排序顺序打印even_indexes列表中的元素。

下面是实现:

C++
// C++ implementation of above approach 
#include
using namespace std;
  
// function to prin the odd and even indexed digits
void odd_even(int arr[], int n)
{
      
    // lists to store the odd and
    // even positioned digits
    vector odd_indexes;
    vectoreven_indexes;
      
    // traverse through all the indexes 
    // in the integer
    for (int i = 0; i < n;i++)
    {
  
        // if the digit is in odd_index position
        // append it to odd_position list
        if (i % 2 == 0)
        odd_indexes.push_back(arr[i]);
          
        // else append it to the even_position list
        else
        even_indexes.push_back(arr[i]);
  
    }
          
    // print the elements in the list in sorted order
    sort(odd_indexes.begin(), odd_indexes.end());
    sort(even_indexes.begin(), even_indexes.end());
      
    for(int i = 0; i < odd_indexes.size();i++)
        cout << odd_indexes[i] << " ";
  
    for(int i = 0; i < even_indexes.size(); i++)
        cout << even_indexes[i] << " "; 
      
} 
  
// Driver code
int main()
{
    int arr[] = {3, 2, 7, 6, 8};
    int n = sizeof(arr)/sizeof(arr[0]);
    odd_even(arr, n);
}
  
// This code is contributed by
// Surendra_Gangwar


Java
// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
// function to prin the odd and even indexed digits
static void odd_even(int arr[], int n)
{
      
    // lists to store the odd and
    // even positioned digits
    Vector odd_indexes = new Vector();
    Vector even_indexes = new Vector();
      
    // traverse through all the indexes 
    // in the integer
    for (int i = 0; i < n;i++)
    {
  
        // if the digit is in odd_index position
        // append it to odd_position list
        if (i % 2 == 0)
            odd_indexes.add(arr[i]);
          
        // else append it to the even_position list
        else
            even_indexes.add(arr[i]);
  
    }
          
    // print the elements in the list in sorted order
    Collections.sort(odd_indexes);
    Collections.sort(even_indexes);
      
    for(int i = 0; i < odd_indexes.size(); i++)
        System.out.print(odd_indexes.get(i) + " ");
  
    for(int i = 0; i < even_indexes.size(); i++)
        System.out.print(even_indexes.get(i) + " "); 
      
} 
  
// Driver code
public static void main(String[] args)
{
    int arr[] = {3, 2, 7, 6, 8};
    int n = arr.length;
    odd_even(arr, n);
}
}
  
// This code is contributed by Rajput-Ji


Python3
# function to prin the odd and even indexed digits
def odd_even(n):
      
    # lists to store the odd and
    # even positioned digits
    odd_indexes = []
    even_indexes = []
      
    # traverse through all the indexes 
    # in the integer
    for i in range(len(n)):
          
        # if the digit is in odd_index position
        # append it to odd_position list
        if i % 2 == 0: odd_indexes.append(n[i])
          
        # else append it to the even_position list
        else: even_indexes.append(n[i])
          
    # print the elements in the list in sorted order
    for i in sorted(odd_indexes): print(i, end =" ")
    for i in sorted(even_indexes): print(i, end =" ")
  
  
# Driver Code
n = [3, 2, 7, 6, 8]
odd_even(n)


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG 
{
  
// function to prin the odd and even indexed digits
static void odd_even(int []arr, int n)
{
      
    // lists to store the odd and
    // even positioned digits
    List odd_indexes = new List();
    List even_indexes = new List();
      
    // traverse through all the indexes 
    // in the integer
    for (int i = 0; i < n;i++)
    {
  
        // if the digit is in odd_index position
        // append it to odd_position list
        if (i % 2 == 0)
            odd_indexes.Add(arr[i]);
          
        // else append it to the even_position list
        else
            even_indexes.Add(arr[i]);
  
    }
          
    // print the elements in the list in sorted order
    odd_indexes.Sort();
    even_indexes.Sort();
      
    for(int i = 0; i < odd_indexes.Count; i++)
        Console.Write(odd_indexes[i] + " ");
  
    for(int i = 0; i < even_indexes.Count; i++)
        Console.Write(even_indexes[i] + " "); 
      
} 
  
// Driver code
public static void Main(String[] args)
{
    int []arr = {3, 2, 7, 6, 8};
    int n = arr.Length;
    odd_even(arr, n);
}
}
  
// This code is contributed by PrinciRaj1992


PHP


输出:
3 7 8 2 6

时间复杂度: O(nlogn)