1
기수 정렬을 사용하여 배열에서 일부 플로트 데이터를 정렬하는 방법은 무엇입니까? 나는 모든 데이터를 정수로 만드는 10의 가장 작은 제곱으로 곱해야한다고 생각합니다. 그러나 나는 그 적절한 힘을 어떻게 이해할 수 있는지 모른다. 이것은 정수 배열을 정렬하기위한 C++ 코드입니다. 누구든지이 일을 도와 줄 수 있습니까? NAN 같은 특수 번호를 제외하고플로트 숫자 배열에 기수 정렬을 사용하려면 어떻게해야합니까?
#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;
}
이 기능은 보편적으로 작동합니까 아니면 IEEE 플로트에서만 작동합니까? 또한 NAN 및 기타 특수 값은 어떻게됩니까? 마지막으로, 비정규 숫자를 올바르게 처리합니까? –
@JimMischel -이 메서드는 IEEE 부동 소수점, 특히 부호와 크기에 해당하는 부동 소수점을 의미합니다. 비정규 화 된 숫자는 정상적으로 작동합니다. 특수 값은 지수 값에 따라 앞이나 뒤로 정렬됩니다. – rcgldr