JIYIK CN >

Current Location:Home > Learning > ALGORITHM >

The road to learning sorting algorithms - Radix Sort (MSD)

Author:JIYIK Last Updated:2025/03/19 Views:

In the article "Radix Sort (LSD)" , we explained the concept and efficiency analysis of radix sort. We will not repeat it in this article. You can refer to that article to get a general understanding of the idea of ​​radix sort.

Next, we will directly introduce the steps of MSD radix sort.

MSD radix sorting groups the sequence from the highest digit to the lowest digit. However, its implementation is different from LSD radix sorting, and a recursive function is required in the sorting process.

Sequence to be sorted

170, 45, 75, 90, 2, 24, 802, 66

We can see that the largest number here is a 3-digit number. So we start to group these numbers from the hundreds place

0: 045, 075, 090,002, 024, 066
1: 170
2-7: Empty
8: 802
9: Empty

From here on, it is different from LSD radix sort. In LSD radix sort, after each grouping, the data in the bucket is collected. Then the collected sequence is grouped according to the next bit. For MSD, the data in the bucket is not collected here. What we need to do is to check the data in each bucket. When the number of elements in the bucket is more than 1, the bucket should be recursively grouped for the next bit.

In this example, we want to group all the elements in bucket 0 according to the digits in the tens place.

0: 002
1: Empty
2: 024
3: Empty
4: 045
5: Empty
6: 066
7: 075
8: Empty
9: 090

According to what is said above, we should recursively group the elements in each bucket according to the number in the ones place. However, we find that the number of elements in each bucket is less than or equal to 1. Therefore, we start to back off at this step. In other words, we start to collect the data in the bucket. After the collection is completed, we back off to the previous level. At this time, the buckets grouped according to the hundreds place become as follows

0: 002, 024, 045,066, 075, 090
1: 170
2-7: Empty
8: 802
9: Empty

Then we collect the data in this bucket. After collection, the sequence is as follows

2, 24, 45, 66, 75, 90, 170, 802

The entire MSD cardinality sorting is performed according to the above process.

When I described the steps of MSD radix sorting, the process of the intermediate recursive function may not be described clearly enough. I personally suggest that those who don’t understand recursive functions can first understand the principle of recursive functions, and then come back to this process, which may make it easier to understand the MSD radix sorting process.

Of course, a sorting algorithm needs to be implemented in code. Here is the PHP code for MSD radix sorting.

/**
 * 找到序列中最大位数
 */
function FindMaxBit($arr){
    /*
     * 首先查找序列中最大的数
     */
    $p = $arr[0];
    for($i=1;$i<count($arr);$i++){
        if($arr[$i]>=$p){
            $p = $arr[$i];
        }
    }
    //得到最大的数以后,计算出该数据有多少位
    $d = 1;
    while(floor($p/10)>0){
        $d++;
        $p = floor($p/10);
    }
    return $d;
}
/**
 * MSD基数排序函数
 * @param array $arr  待分组的序列
 * @param array $r    表示当前是按照第几位进行分组
 */
function MSD_RadixSort(&$arr,$r){
    $radix = pow(10,$r-1);
    $bucket = array();
    //初始化队列
    for($i=0;$i<10;$i++){
        $bucket[$i]=array('num'=>0,'val'=>array());
    }
    for($j=0;$j<count($arr);$j++){
        $k = floor($arr[$j]%pow(10,$r)/$radix);
        $bucket[$k]['num']++;
        array_push($bucket[$k]['val'],$arr[$j]);
    }
    for($j=0;$j<10;$j++){
        if($bucket[$j]['num']>1){
            MSD_RadixSort($bucket[$j]['val'],$r-1);
        }
    }
    /*
     * 将桶中的数据收集起来,此时序列是有序的
     */
    $arr = array();
    for($j=0;$j<10;$j++){
        for($i=0;$i<count($bucket[$j]['val']);$i++){
            $arr[] = $bucket[$j]['val'][$i];
        }
    }
}
$arr = array(
    15,77,23,43,90,87,68,32,11,22,33,99,88,66,44,113,
    224,765,980,159,456,7,998,451,96,0,673,82,91,100
);
MSD_RadixSort($arr,FindMaxBit($arr));
print_r($arr);

I hope this article is helpful to you.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Java 中的选择排序算法

Publish Date:2023/10/17 Views:154 Category:Java

本教程演示了 Java 中的选择排序算法。选择排序是首先选择列表或数组中最小的元素并与第一个元素或数组交换的方法;然后,第二个缩小的元素与第二个元素交换。

Java 基数排序算法

Publish Date:2023/10/17 Views:206 Category:Java

本教程详细解释了基数排序算法并演示了 Java 中的实现。在基数排序算法中,元素的排序首先将具有相同位值的单个数字分组,然后按照升序或降序排序。本教程详细解释了基数排序算法,并演

C++ 中最快的排序算法

Publish Date:2023/08/31 Views:164 Category:C++

本文将解释哪种排序算法在什么条件下表现最好。 条件包括数据结构的类型、排序数据的大小、数据排列和数据元素的范围。

Java最快的排序算法

Publish Date:2023/07/16 Views:166 Category:Java

本文将介绍两种最快的排序算法并用 Java 编写它们的代码。第一种技术是计数排序,它有一些局限性。 因此,我们还将介绍合并排序算法。 Java中的计数排序算法 Java中的归并排序算法

全面了解归并排序算法及代码实现

Publish Date:2021/08/19 Views:327 Category:算法

归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。这里拆分过程的代码可以分为两种方式:递归实现和非递归实现

排序算法学习之路——基数排序(MSD)

Publish Date:2016/04/14 Views:6650 Category:算法

MSD基数排序是从最高位开始对序列进行分组,到最低位为止。但是其实现过程是和LSD基数排序不同的,排序过程中需要用到递归函数。

排序算法学习之路——基数排序(LSD)

Publish Date:2016/04/14 Views:7666 Category:算法

基数排序的基本原理是,按照整数的每个位数分组。在分组过程中,对于不足位的数据用0补位。基数排序按照对位数分组的顺序的不同,可以分为LSD基数排序和MSD基数排序。

常用排序算法的核心代码

Publish Date:2016/04/13 Views:320 Category:算法

主要对常用排序算法的核心代码的实现,代码都是使用PHP实现的。完整代码托管在github上

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial