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

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

根据小数值对给定数组进行排序

给定一个包含N个实数的数组arr[] 。任务是按分数值的降序对数组进行排序

注意:如果小数部分的值相同,则按Integer Values的递减顺序对这些元素进行排序。

例子:

Naive Approach:解决问题的基本思路是将{Fractional value, Integer value}这对存储在一个向量中,然后按小数部分的降序对向量进行排序。最后反转最终答案的向量,因为所需的答案应该按降序排列。

下面是上述方法的实现:

C++
// C++ program to sort Array
// in descending order of fractional value
 
#include 
using namespace std;
 
// Function to sort the fractional part
void SortFraction(long double arr[], int n)
{
 
    // To store fraction and it's
    // corresponding integer and the
    // original element
    vector,
                long double> >
        v;
    for (int i = 0; i < n; i++) {
 
        // Calculate fractional value
        long double fraction
            = arr[i] - floorl(arr[i]);
 
        // Calculate integer value
        int integer = floorl(arr[i]);
 
        v.push_back({ make_pair(fraction,
                                integer),
                      arr[i] });
    }
 
    // Sort the vector
    sort(v.begin(), v.end());
 
    // To get final answer,
    // reverse the vector
    reverse(v.begin(), v.end());
 
    // To print output
    for (int i = 0; i < n; i++)
        cout << v[i].second << " ";
}
 
// Driver Code
int main()
{
    long double arr[] = { 8.33, -3.85,
                          1.999, 6.33, 5 };
 
    // Size of array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Sort in descending order
    // of fractional value
    SortFraction(arr, N);
    return 0;
}


Javascript


C++
// C++ program to sort Array
// in descending order of fractional value
 
#include 
using namespace std;
 
// Comparator to sort array
// according to question
bool comp(long double a, long double b)
{
    int int_a = floorl(a);
    int int_b = floorl(b);
 
    long double fraction_a = a - int_a;
    long double fraction_b = b - int_b;
 
    if (fraction_a > fraction_b)
        return true;
 
    if (fraction_a == fraction_b) {
        if (int_a > int_b)
            return true;
        else
            return false;
    }
    return false;
}
 
// Function to print answer
void print(long double arr[], int n)
{
    // Sort in descending order of
    // fractional value pass comp to sort
    sort(arr, arr + n, comp);
 
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Driver Code
int main()
{
    long double arr[]
        = { 8.33, -3.85, 1.999, 6.33, 5 };
 
    // Size of arr
    int N = sizeof(arr) / sizeof(arr[0]);
    print(arr, N);
    return 0;
}


输出
1.999 8.33 6.33 -3.85 5 

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

高效方法:以优化方式解决问题的想法是使用比较器对向量进行排序。

请按照以下步骤解决问题:

  • 首先,制作一个比较器函数,根据问题中给出的要求对数组进行排序。
    • 如果a 的分数> b的分数,则返回true ,以按小数值的降序对元素进行排序。
    • 如果a 的小数= b的小数 如果a 的整数 > b的整数,则返回true
    • 在其他情况下返回False
  • 最终数组是所需的排序数组。

下面是上述方法的实现:

C++

// C++ program to sort Array
// in descending order of fractional value
 
#include 
using namespace std;
 
// Comparator to sort array
// according to question
bool comp(long double a, long double b)
{
    int int_a = floorl(a);
    int int_b = floorl(b);
 
    long double fraction_a = a - int_a;
    long double fraction_b = b - int_b;
 
    if (fraction_a > fraction_b)
        return true;
 
    if (fraction_a == fraction_b) {
        if (int_a > int_b)
            return true;
        else
            return false;
    }
    return false;
}
 
// Function to print answer
void print(long double arr[], int n)
{
    // Sort in descending order of
    // fractional value pass comp to sort
    sort(arr, arr + n, comp);
 
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Driver Code
int main()
{
    long double arr[]
        = { 8.33, -3.85, 1.999, 6.33, 5 };
 
    // Size of arr
    int N = sizeof(arr) / sizeof(arr[0]);
    print(arr, N);
    return 0;
}
输出
1.999 8.33 6.33 -3.85 5 

时间复杂度: O(N * logN)
辅助空间: O(1)