📜  根据因素数量对元素进行排序

📅  最后修改于: 2021-04-23 18:38:08             🧑  作者: Mango

给定正整数数组。以每个元素的因子数量降序对给定数组进行排序,即,因子数量最多的元素应该是第一个显示,因子数量最少的元素应该是最后一个显示。具有相同数量因子的两个元素应与原始数组中的顺序相同。

例子:

Input : {5, 11, 10, 20, 9, 16, 23}
Output : 20 16 10 9 5 11 23
Number of distinct factors:
For 20 = 6, 16 = 5, 10 = 4, 9 = 3
and for 5, 11, 23 = 2 (same number of factors
therefore sorted in increasing order of index)

Input : {104, 210, 315, 166, 441, 180}
Output : 180 210 315 441 104 166

以下步骤按因子计数的降序对数字进行排序。

  1. 计算每个元素的不同数量的因素。请参考此。
  2. 您可以为每个元素使用结构来存储其原始索引和因子计数。创建此类结构的数组以存储所有元素的此类信息。
  3. 使用任何排序算法,根据问题陈述对这个结构数组进行排序。
  4. 从头开始遍历此结构数组,并借助存储在已排序结构数组的每个元素的结构中的索引,从原始数组中获取数字。
C++
// C++ implementation to sort numbers on
// the basis of factors
#include 
  
using namespace std;
  
// structure of each element having its index
// in the input array and number of factors
struct element
{
    int index, no_of_fact;
};
  
// function to count factors for
// a given number n
int countFactors(int n)
{
    int count = 0;
    int sq = sqrt(n);
      
    // if the number is a perfect square
    if (sq * sq == n)
        count++;
      
    // count all other factors
    for (int i=1; i e2.no_of_fact;    
}
  
// function to print numbers after sorting them in
// decreasing order of number of factors
void printOnBasisOfFactors(int arr[], int n)
{    
    struct element num[n];
      
    // for each element of input array create a
    // structure element to store its index and 
    // factors count 
    for (int i=0; i


Java
// Java implementation to sort numbers on
// the basis of factors
  
import java.util.Arrays;
import java.util.Comparator;
  
class Element 
{
    //each element having its index
    // in the input array and number of factors
    int index, no_of_fact;
   
    public Element(int i, int countFactors)
    {
        index = i;
        no_of_fact = countFactors;
    }
  
    // method to count factors for
    // a given number n
    static int countFactors(int n)
    {
        int count = 0;
        int sq = (int)Math.sqrt(n);
       
        // if the number is a perfect square
        if (sq * sq == n)
            count++;
       
        // count all other factors
        for (int i=1; i() {
  
            @Override
            // compare method for the elements 
            // of the structure
            public int compare(Element e1, Element e2) {
                // if two elements have the same number
                // of factors then sort them in increasing
                // order of their index in the input array
                if (e1.no_of_fact == e2.no_of_fact)
                 return e1.index < e2.index ? -1 : 1;
                
                // sort in decreasing order of number of factors
                return e1.no_of_fact > e2.no_of_fact ? -1 : 1;  
            }
              
        });
       
        // access index from the structure element and correponding
        // to that index access the element from arr
        for (int i=0; i


输出:

20 16 10 9 5 11 23

时间复杂度:O(n&Sqrt; n)