📌  相关文章
📜  根据给定条件对给定数组进行排序

📅  最后修改于: 2022-05-13 01:56:07.343000             🧑  作者: Mango

根据给定条件对给定数组进行排序

给定一个由N个正整数组成的数组arr[] ,任务是对数组进行排序,使得 -

  • 所有偶数必须在所有奇数之前。
  • 所有能被 5 整除的偶数都必须排在不能被 5 整除的偶数之前。
  • 如果两个偶数能被 5 整除,那么值大的数会排在前面
  • 如果两个偶数不能被 5 整除,则数组中具有更大索引的数字将排在第一位。
  • 所有奇数必须按相对顺序排列,因为它们存在于数组中。

例子:

方法:这个问题可以通过使用排序来解决。请按照以下步骤解决问题:

  • 创建三个向量,例如v1v2v3 ,其中v1存储偶数且可被5整除的数字, v2存储偶数但不能被5整除的数字, v3存储奇数。
  • 使用变量i[0, N-1]范围内迭代并执行以下步骤 -
    • 如果arr[i]%2 = 0arr[i]%5=0 ,则在v1中插入arr[i]
    • 如果arr[i]%2 = 0arr[i]%5 != 0,则在v2中插入arr[i]
    • 如果arr[i]%2则在v3中插入 arr[i]
  • 按降序对向量v1进行排序。
  • 执行上述步骤后,打印向量v1 ,然后打印向量v2 ,然后打印v3作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to sort array in the way
// mentioned above
void AwesomeSort(vector m, int n)
{
    // Create three vectors
    vector v1, v2, v3;
 
    int i;
 
    // Traverse through the elements
    // of the array
    for (i = 0; i < n; i++) {
 
        // If elements are even and
        // divisible by 10
        if (m[i] % 10 == 0)
            v1.push_back(m[i]);
 
        // If number is even but not divisible
        // by 5
        else if (m[i] % 2 == 0)
            v2.push_back(m[i]);
        else
            // If number is odd
            v3.push_back(m[i]);
    }
 
    // Sort  v1 in descending orde
    sort(v1.begin(), v1.end(), greater());
 
    for (int i = 0; i < v1.size(); i++) {
        cout << v1[i] << " ";
    }
    for (int i = v2.size()-1; i >= 0; i--) {
        cout << v2[i] << " ";
    }
    for (int i = 0; i < v3.size(); i++) {
        cout << v3[i] << " ";
    }
}
 
// Driver Code
int main()
{
    // Given Input
    vector arr{ 5, 10, 30, 7 };
    int N = arr.size();
 
    // FunctionCall
    AwesomeSort(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.Collections;
import java.util.Vector;
 
public class GFG_JAVA {
 
    // Function to sort array in the way
    // mentioned above
    static void AwesomeSort(Vector m, int n)
    {
        // Create three vectors
        Vector v1 = new Vector<>();
        Vector v2 = new Vector<>();
        Vector v3 = new Vector<>();
 
        int i;
 
        // Traverse through the elements
        // of the array
        for (i = 0; i < n; i++) {
 
            // If elements are even and
            // divisible by 10
            if (m.get(i) % 10 == 0)
                v1.add(m.get(i));
 
            // If number is even but not divisible
            // by 5
            else if (m.get(i) % 2 == 0)
                v2.add(m.get(i));
            else
                // If number is odd
                v3.add(m.get(i));
        }
 
        // Sort  v1 in descending orde
        Collections.sort(v1, Collections.reverseOrder());
 
        for (int ii = 0; ii < v1.size(); ii++) {
            System.out.print(v1.get(ii) + " ");
        }
        for (int ii = v2.size()-1; ii >= 0; ii--) {
            System.out.print(v2.get(ii) + " ");
        }
        for (int ii = 0; ii < v3.size(); ii++) {
            System.out.print(v3.get(ii) + " ");
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // Given Input
        Vector arr = new Vector<>();
        arr.add(5);
        arr.add(10);
        arr.add(30);
        arr.add(7);
 
        int N = arr.size();
 
        // FunctionCall
        AwesomeSort(arr, N);
    }
}
 
// This code is contributed by abhinavjain194


Python3
# Python program for the above approach
 
# Function to sort array in the way
# mentioned above
def AwesomeSort(m, n):
   
    # Create three vectors
    v1, v2, v3 = [],[],[]
     
    # Traverse through the elements
    # of the array
    for i in range(n):
       
        # If elements are even and
        # divisible by 10
        if (m[i] % 10 == 0):
            v1.append(m[i])
 
        # If number is even but not divisible
        # by 5
        elif (m[i] % 2 == 0):
            v2.append(m[i])
        else:
            # If number is odd
            v3.append(m[i])
 
    # Sort  v1 in descending orde
    v1 = sorted(v1)[::-1]
 
    for i in range(len(v1)):
        print(v1[i], end = " ")
 
    for i in range(len(v2)):
        print(v2[i], end = " ")
 
    for i in range(len(v3)):
        print (v3[i], end = " ")
 
# Driver Code
if __name__ == '__main__':
   
    # Given Input
    arr = [5, 10, 30, 7]
    N = len(arr)
 
    # FunctionCall
    AwesomeSort(arr, N)
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
     
    // Function to sort array in the way
    // mentioned above
    static void AwesomeSort(List m, int n)
    {
       
        // Create three vectors
        List v1 = new List();
        List v2 = new List();
        List v3 = new List();
  
        int i;
  
        // Traverse through the elements
        // of the array
        for (i = 0; i < n; i++) {
  
            // If elements are even and
            // divisible by 10
            if (m[i] % 10 == 0)
                v1.Add(m[i]);
  
            // If number is even but not divisible
            // by 5
            else if (m[i] % 2 == 0)
                v2.Add(m[i]);
            else
                // If number is odd
                v3.Add(m[i]);
        }
  
        // Sort  v1 in descending orde
        v1.Sort();
        v1.Reverse();
  
        for (int ii = 0; ii < v1.Count; ii++) {
            Console.Write(v1[ii] + " ");
        }
        for (int ii = 0; ii < v2.Count; ii++) {
            Console.Write(v2[ii] + " ");
        }
        for (int ii = 0; ii < v3.Count; ii++) {
            Console.Write(v3[ii] + " ");
        }
    }
     
  static void Main()
  {
     
    // Given Input
    List arr = new List();
    arr.Add(5);
    arr.Add(10);
    arr.Add(30);
    arr.Add(7);
 
    int N = arr.Count;
 
    // FunctionCall
    AwesomeSort(arr, N);
  }
}
 
// This code is contributed by divyeshrabadiya07.


Javascript


输出
30 10 5 7 

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