Я, наконец, получил сортировку оснований на десятичной основе, чтобы работать, но хочу превратить ее в одну, которая сортирует биты.
void LSD_radixSort (unsigned int * A, int size, int r)
{
// Find the maximum number to know number of digits
int m = getMax(A, size);
// 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; m/exp > 0; exp *= 10)
countSort2(A, size, exp);
}
unsigned int getMax(unsigned int arr[], int n)
{
unsigned int mx = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
void countSort2(unsigned int arr[], int n, int exp)
{
int *output = new int[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--)
{
output[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 curent digit
for (i = 0; i < n; i++)
arr[i] = output[i];
i = 0;
}
не меняя параметры моей функции LSD_radixSort, как бы я мог преобразовать ее, когда r является моим показателем для 2 ^ r - 1?
Любые предложения на всех были бы весьма признательны!
По существу вам нужно узнать о побитовых операторах.
То, что вам нужно знать для сортировки radix, состоит в том, что 2 ^ n равно 1 << n
и что в сочетании с поразрядным и &
вы можете запросить, если n-й бит установлен в x & (1 << n) != 0
.