C ++ radix sort

Bucket sort is a radix sort, belonging to the sort stability.

example

Los sort Valley 1177

Description Title
number N of the read back output in ascending order.

Input format
line 1 is a positive integer N.
The second number of rows separated by a space comprising N a positive integer a [i], you need to sort, to ensure that the data a [i] is not more than 10 ^ 9. Output format of the given number N output from small to large, numbers separated by spaces.


Sample Input Output
Input 1

5
4 2 4 5 1

Output 1

1 2 4 4 5

DESCRIPTION Tip
20% to the data, there are N <= 10 ^ 3.
To 100% of the data, there are N <= 10 ^ 5.

Radix Sort

Radix sort often used numbers on every sort, but this can only be on a non-negative integer sort (the reason why this problem can radix sort AC, because all the data is not negative, otherwise collapse) . First algorithm is performed on the bucket sort of digital bits for each element, i.e. an array of open Base [i] [j] indicates the j-th value of all digits of the number i, followed by i from 0 to 9 these numbers back in order again in the array. When then again by ten, one hundred, one thousand so down-digit row until discharged median is the largest element of the array, it is the sort is complete.

For example, to facilitate understanding.
Initial state: 712,303,4,18,89,999,70,26
first sorted by bit: 07 0 , 71 2 and 30 . 3 00 . 4 , 02 . 6 , 01 . 8 08 . 9 , 99 . 9
first Sort by ten secondary: 3 0 3,0 0 4,7 1 2,0 1 8,0 2 6 0 . 7 0,0 8 9,9 . 9 . 9
third Sort by one hundred: 0 04, 0 18, 0 26 0 70 0 89 . 3 03 . 7 12 . 9 99

Finally, count the time complexity of the algorithm: triple loop, the first loop is heavy digit digit (here a digit number of digits in the largest element of the array, and because the array is a type int, so no digit is connected to 10, is a very small number), the second heavy cycle will add a third heavy cycle n times in total, the time complexity is O (digit * n) level, very fast.

Code

# include <cstdio>
# include <algorithm>
# include <cmath>
# include <cstring>

using namespace std;

const int N_MAX = 100000;

int n;
int a[N_MAX + 10];
int base[10][N_MAX + 10];

int countMaxDigit(){
    int cnt = 0, num = 0;
    for (int i = 1; i <= n; i++)
        num = max(a[i], num);
    while (num != 0) {
        cnt++;
        num /= 10;
    }
    if (cnt == 0) cnt = 1; // 如果说num = 0,那么位数也应当是1 
    return cnt;
}

void radixSort()
{
    int digit = countMaxDigit(), ten = 1;
    for (int t = 1; t <= digit; t++) {
        memset(base, 0, sizeof(base));
        for (int i = 1; i <= n; i++) {
            int num = a[i] / ten % 10;
            base[num][++base[num][0]] = a[i]; // 这里直接用base[num][0]表示base[num]中元素的个数了
        }
        int now = 0;
        for (int i = 0; i <= 9; i++)
            for (int j = 1; j <= base[i][0]; j++)
                a[++now] = base[i][j];
        ten *= 10;
    }
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    radixSort();
    for (int i = 1; i <= n; i++)
        printf("%d ", a[i]);
    printf("\n");
    return 0;
}

Guess you like

Origin www.cnblogs.com/000zwx000/p/12330710.html