根据小数值对给定数组进行排序
给定一个包含N个实数的数组arr[] 。任务是按分数值的降序对数组进行排序
注意:如果小数部分的值相同,则按Integer Values的递减顺序对这些元素进行排序。
例子:
Input: arr[] = { 8.33, -3.85, 1.999, 6.33, 5}
Output: { 1.999, 8.33, 6.33, -3.85 , 5}
Explanation:
Element | Integer Value | Fraction Value |
8.33 | 8 | 0.33 |
-3.85 | -4 | 0.15 |
1.999 | 1 | 0.999 |
6.33 | 6 | 0.33 |
5 | 5 | 0.0 |
1.999 has the biggest fractional value so it will be at index 0 while 5 has the lowest fractional value so it will be at the last index.
6.33 and 8.33 have the same fractional values but the Integer part of 8.33 is greater than 6.33 so we place 8.33 before 6.33 as the given condition in the problem statement.
Fractional value = Given number – Integer Value.
Fractional Value of positive value e.g. (8.33) = 8.33 – Integer Value of ( 8.33)= 8.33 – 8= 0.33
Fractional Value of (-3.85) = -3.85- Integer Value of (-3.85)= -3.85 – ( -4 ) = -3.85 + 4 = 0.15
Hence, the order for the above example will be: {1.999, 8.33, 6.33, -3.85, 5}
Input: arr[] = { 1.1, 1.11, 1.111, 2.1, 2.11, 2.111}
Output: { 2.111, 1.111, 2.11, 1.11, 2.1, 1.1 }
Explanation: Here the biggest fractional value is 0.111 which is present in both 2.111 and 1.111. But the integer value of 2.111 is greater than 1.111. so, 2.111 will come before 1.111. The same applies to other elements.
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)