Data structure and algorithm-Graphical Radix Sort algorithm and C/C++ code implementation [Recommended collection]

1. Introduction to Cardinal Sort

Radix Sort (Radix Sort) is an extension of bucket sorting. Its basic idea is to cut integers into different numbers by digits, and then compare them according to each digit.
The specific method is: unify all the values ​​to be compared to the same digit length, and padded zeros in front of the numbers with shorter digits. Then, starting from the lowest bit, sort one time in sequence. So after sorting from the lowest order to the highest order, the sequence becomes an ordered sequence.

2. Graphical description of cardinal sorting

Sort the array {53, 3, 542, 748, 14, 214, 154, 63, 616} by radix sorting. Its schematic diagram is as follows:
Insert picture description here
In the above figure, first unify all resins to be compared to a uniform digit length, and then Starting from the lowest bit, sort in order.

  1. Sort according to the single digit.
  2. Sort by tens.
  3. Sort by hundred digits.
    After sorting, the sequence becomes an ordered sequence.

Base sort code

/*
 * 获取数组a中最大值
 *
 * 参数说明:
 *     a -- 数组
 *     n -- 数组长度
 */
int get_max(int a[], int n)
{
    
    
    int i, max;

    max = a[0];
    for (i = 1; i < n; i++)
        if (a[i] > max)
            max = a[i];
    return max;
}

/*
 * 对数组按照"某个位数"进行排序(桶排序)
 *
 * 参数说明:
 *     a -- 数组
 *     n -- 数组长度
 *     exp -- 指数。对数组a按照该指数进行排序。
 *
 * 例如,对于数组a={50, 3, 542, 745, 2014, 154, 63, 616};
 *    (01) 当exp=1表示按照"个位"对数组a进行排序
 *    (02) 当exp=10表示按照"十位"对数组a进行排序
 *    (03) 当exp=100表示按照"百位"对数组a进行排序
 *    ...
 */
void count_sort(int a[], int n, int exp)
{
    
    
    int output[n];             // 存储"被排序数据"的临时数组
    int i, buckets[10] = {
    
    0};

    // 将数据出现的次数存储在buckets[]中
    for (i = 0; i < n; i++)
        buckets[ (a[i]/exp)%10 ]++;

    // 更改buckets[i]。目的是让更改后的buckets[i]的值,是该数据在output[]中的位置。
    for (i = 1; i < 10; i++)
        buckets[i] += buckets[i - 1];

    // 将数据存储到临时数组output[]中
    for (i = n - 1; i >= 0; i--)
    {
    
    
        output[buckets[ (a[i]/exp)%10 ] - 1] = a[i];
        buckets[ (a[i]/exp)%10 ]--;
    }

    // 将排序好的数据赋值给a[]
    for (i = 0; i < n; i++)
        a[i] = output[i];
}

/*
 * 基数排序
 *
 * 参数说明:
 *     a -- 数组
 *     n -- 数组长度
 */
void radix_sort(int a[], int n)
{
    
    
    int exp;    // 指数。当对数组按各位进行排序时,exp=1;按十位进行排序时,exp=10;...
    int max = get_max(a, n);    // 数组a中的最大值

    // 从个位开始,对数组a按"指数"进行排序
    for (exp = 1; max/exp > 0; exp *= 10)
        count_sort(a, n, exp);
}

The function of radix_sort(a, n) is to sort the array a.

  1. First, get the maximum value in the array a through get_max(a). The purpose of obtaining the maximum value is to calculate the maximum index of the array a.

  2. After obtaining the largest exponent in the array a, starting from the exponent 1, sort the elements in the array a according to the number of bits. Bucket sorting is used when sorting.

  3. The function of count_sort(a, n, exp) is to sort the array a according to the exponent exp.
    The following briefly introduces the process of sorting the array {53, 3, 542, 748, 14, 214, 154, 63, 616} by single digits.
    (01) The value range of the ones place is [0,10). Therefore, refer to the bucket array buckets[] and add the array to the bucket according to the unit digit value.
    Insert picture description here
    (02) Next, sort according to the bucket array buckets[]. Suppose that the sorted array is stored in output[]; the data can be sorted by finding the connection between output[] and buckets[].
    Insert picture description here
    The editor recommends my own linuxC/C++ language technology exchange group: [ 1106675687 ] I have compiled some learning books and video materials that I think are better to share in the group files, and you can add them if you need them!
    Insert picture description here

Third, the radix sort implementation

Radix sort C implementation
implementation code (radix_sort.c)

 View Code

Radix Sort C++ Implementation
Implementation Code (RadixSort.cpp)

 View Code

Radix Sort Java Implementation
Implementation Code (RadixSort.java)

 View Code

The principles and output results of the above three implementations are the same. Here is their output:

before sort:53 3 542 748 14 214 154 63 616 
after  sort:3 14 53 63 154 214 542 616 748 

Guess you like

Origin blog.csdn.net/m0_50662680/article/details/113056302