改变基数排序基数?

时间:2019-04-25 00:18:40

标签: c++ sorting radix-sort

我试图理解基数排序,但是在理解实现实际代码的基础变更时遇到了问题。这是我用来学习基数排序的代码,我将尝试解释我不了解的内容。

此代码由GeeksForGeeks编写:

// C++ implementation of Radix Sort 
#include<iostream> 
using namespace std; 

// A utility function to get maximum value in arr[] 
int getMax(int arr[], int n) 
{ 
    int mx = arr[0]; 
    for (int i = 1; i < n; i++) 
        if (arr[i] > mx) 
            mx = arr[i]; 
    return mx; 
} 

// A function to do counting sort of arr[] according to 
// the digit represented by exp. 
void countSort(int arr[], int n, int exp) 
{ 
    int output[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 current digit 
    for (i = 0; i < n; i++) 
        arr[i] = output[i]; 
} 

// The main function to that sorts arr[] of size n using  
// Radix Sort 
void radixsort(int arr[], int n) 
{ 
    // Find the maximum number to know number of digits 
    int m = getMax(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; m/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] << " "; 
} 

// Driver program to test above functions 
int main() 
{ 
    int arr[] = {170, 45, 75, 90, 802, 24, 2, 66}; 
    int n = sizeof(arr)/sizeof(arr[0]); 
    radixsort(arr, n); 
    print(arr, n); 
    return 0; 
} 

所以我遇到的一个问题是,我需要在用户选择其基数的地方进行基数可变的排序。我的理解是,基数只是函数的表示形式,但不确定如何将其实现为基数排序。如果我继续使用基数10,它将对排序算法有何影响(在复杂度之外)?

2 个答案:

答案 0 :(得分:2)

您拥有的代码适用于10级。每次在以10为基数的代码中看到硬编码10时,要对其进行更改,就需要使每个事件动态化。

基数排序的复杂度不取决于基数,而是始终为O(kn)[键的长度*键的n]。更改基数有助于减少执行排序所需的通过次数,但会增加每次通过中计算的存储桶数量。除此之外,任何碱基都将排序并产生相同的结果。

答案 1 :(得分:0)

我遇到了这个问题,现在没有正确的答案。

首先要注意的是:

这可能不是Radix的最佳算法,但这是问题的答案。

//b is the base you want
//exp is the value used for the division
void counting_sort(int* A, int n, int exp, int b) {

    int * C = new int[b];
    int* B = new int[n];

    for (int i = 0; i < b; i++)
    {
        C[i] = 0;
    }

    for (int i = 0; i < n; i++)
    {
        C[(A[i] / exp) % b]++;
    }

    for (int i = 1; i < b; i++)
    {
        C[i] += C[i - 1];
    }

    for (int i = n - 1; i >= 0; i--)
    {
        B[C[(A[i] / exp) % b] - 1] = A[i];
        C[(A[i] / exp) % b]--;
    }

    for (int i = 0; i < n; i++)
    {
        A[i] = B[i];
    }

    delete[] B;
    delete[] C;
}

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

void radix_sort(int* A, int n) {
    long long max = (long long)getMax(A, n);
    long long base = 8// whatever base you need, I used ll, since long wasn't big enough for my needs (n = 200000).

    for (long long exp = 1; max / exp > 0; exp *= base) {
        counting_sort(A, n, exp, base);
    }
}

基本上就是这样,但是我将添加基数2 ^(log n)

的代码。
long long getBase(int* A, int n) {
    long long log = (long long) log2(n);
    return (long long)pow(2, log);
}

void radix_sort(int* A, int n) {
    long long max = (long long)getMax(A, n);
    long long base = getBase(A, n);
    for (long long exp = 1; max / exp > 0; exp *= base) {
        counting_sort(A, n, exp, base);
    }
}

如果阅读此书的人有任何疑问,请检查我的个人资料。