Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何对浮点数数组使用基数排序?_C++_Arrays_Sorting_Data Structures_Radix Sort - Fatal编程技术网

C++ 如何对浮点数数组使用基数排序?

C++ 如何对浮点数数组使用基数排序?,c++,arrays,sorting,data-structures,radix-sort,C++,Arrays,Sorting,Data Structures,Radix Sort,如何使用基数排序对数组中的某些浮点数据进行排序? 我想我应该把所有的数据乘以最小的10次方,使它们成为整数。但我不知道我怎么能理解这种合适的力量。 这是一个用于排序整数数组的C++代码。 有人能帮我做这个吗 #include<iostream> using namespace std; //Get maximum value in arr[] int findMax(int arr[], int n) { int max = arr[0]; for (in

如何使用基数排序对数组中的某些浮点数据进行排序? 我想我应该把所有的数据乘以最小的10次方,使它们成为整数。但我不知道我怎么能理解这种合适的力量。 这是一个用于排序整数数组的C++代码。 有人能帮我做这个吗

#include<iostream>
using namespace std;
//Get maximum value in arr[]

    int findMax(int arr[], int n)
{
    int max = arr[0];
     for (int i = 1; i < n; i++)
      if (arr[i] > max)
        max = arr[i];
    return max;
}

// A function to do counting sort of arr[] according to
// the digit represented by exp.

    void countSort(int arr[], int n, int exp)
{
    int outputArr[n]; // output array
    int i, count[10] = {0};

// Store count of occurrences in count[]

    for (i = 0; i < n; i++)
      count[ (arr[i]/exp)%10 ]++;

// Change count[i] so that count[i] now contains actual
//  position of this digit in output[]

    for (i = 1; i < 10; i++)
      count[i] += count[i - 1];

// Build the output array

    for (i = n - 1; i >= 0; i--)
   {
      outputArr[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
      count[ (arr[i]/exp)%10 ]--;
   }

// Copy the output array to arr[], so that arr[] now
// contains sorted numbers according to current digit

    for (i = 0; i < n; i++)
      arr[i] = outputArr[i];
}

// The main function to that sorts arr[] of size n using Radix Sort

    void radixsort(int arr[], int n)
    {

      int max = findMax(arr, n);

// Do counting sort for every digit. Note that instead
// of passing digit number, exp is passed. exp is 10^i
// where i is current digit number

  for (int exp = 1; max/exp > 0; exp *= 10)
    countSort(arr, n, exp);
    }

// A utility function to print an array

    void print(int arr[], int n)
    {
       for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    }

   int main()
 {
    int arr[] = {506,2,41,33,5,965,73};
    int n = sizeof(arr)/sizeof(arr[0]);
    radixsort(arr, n);
    print(arr, n);
    return 0;
 }
#包括
使用名称空间std;
//获取arr[]中的最大值
int findMax(int arr[],int n)
{
int max=arr[0];
对于(int i=1;i最大值)
max=arr[i];
返回最大值;
}
//一个函数,用于根据
//exp表示的数字。
void countSort(int arr[],int n,int exp)
{
int outputArr[n];//输出数组
int i,计数[10]={0};
//在计数[]中存储发生次数的计数
对于(i=0;i=0;i--)
{
outputArr[count[(arr[i]/exp)%10]-1]=arr[i];
计数[(arr[i]/exp)%10]-;
}
//将输出数组复制到arr[],使arr[]立即
//包含根据当前数字排序的数字
对于(i=0;i0;exp*=10)
countSort(arr,n,exp);
}
//打印数组的实用函数
无效打印(整数arr[],整数n)
{
对于(int i=0;icout除了像NAN这样的特殊数字外,出于排序目的,您可以将浮点数视为32位的符号+幅度数。对于基数排序,最简单的方法是将符号+幅度数转换为32位无符号整数,然后在排序后再转换回。示例宏用于从浮点转换为无符号以及从无符号转换为浮点。请注意-0将被视为小于+0,这是一个潜在的稳定性问题,但浮点运算通常不会生成-0,这可以通过检查-0并将其与代码中的+0相同来处理。在使用这些宏之前,将浮点转换为无符号整数

#define FLOAT_2_U(x) ((x)^(((~(x) >> 31)-1) | 0x80000000))
#define U_2_FLOAT(x) ((x)^((( (x) >> 31)-1) | 0x80000000))

这是通用的还是仅适用于IEEE浮点数?此外,NAN和其他特殊值会发生什么情况?最后,这是否正确处理非规范化数字?@JimMischel-此方法适用于IEEE浮点数,特别是与符号和幅值等效的浮点数。非规范化数字应该可以工作。特殊值将被排序根据指数值向前或向后移动。