简体   繁体   English

基数排序说明

[英]Radix sort explanation

Based on this radix sort article http://www.geeksforgeeks.org/radix-sort/ I'm struggling to understand what is being explained in terms of the time complexity of certain methods in the sort. 基于这个基数排序文章http://www.geeksforgeeks.org/radix-sort/我正在努力理解在某种方法的时间复杂性方面正在解释什么。

From the link: 从链接:

Let there be d digits in input integers. 让输入整数有d位数。 Radix Sort takes O(d*(n+b)) time where b is the base for representing numbers, for example, for decimal system, b is 10. What is the value of d? 基数排序采用O(d *(n + b))时间,其中b是表示数字的基数,例如,对于十进制系统,b为10. d的值是多少? If k is the maximum possible value, then d would be O(log_b(k)). 如果k是最大可能值,则d将是O(log_b(k))。 So overall time complexity is O((n+b)*logb(k)). 因此总体时间复杂度为O((n + b)* logb(k))。 Which looks more than the time complexity of comparison based sorting algorithms for a large k. 对于大k来说,这看起来不仅仅是基于比较的排序算法的时间复杂度。 Let us first limit k. 让我们先来限制k。 Let k≤nc where c is a constant. 设k≤nc,其中c是常数。 In that case, the complexity becomes O(nlogb(n)). 在这种情况下,复杂性变为O(nlogb(n))。

So I do understand that the sort takes O(d*n) since there are d digits therefore d passes, and you have to process all n elements, but I lost it from there. 所以我确实理解排序需要O(d * n),因为有d个数字因此d遍,你必须处理所有n个元素,但我从那里丢失了它。 A simple explanation would be really helpful. 一个简单的解释将非常有用。

Assuming we use bucket sort for the sorting on each digit: for each digit (d) , we process all numbers (n) , placing them in buckets for all possible values a digit may have (b) . 假设我们使用桶排序对每个数字进行排序:对于每个数字(d) ,我们处理所有数字(n) ,将它们放入桶中以获得数字可能具有的所有可能值(b)

We then need to process all the buckets, recreating the original list. 然后,我们需要处理所有桶,重新创建原始列表。 Placing all items in the buckets takes O(n) time, recreating the list from all the buckets takes O(n + b) time (we have to iterate over all buckets and all elements inside them), and we do this for all digits, giving a running time of O(d * (n + b)) . 将所有项放入存储桶需要O(n)时间,从所有存储桶重新创建列表需要O(n + b)时间(我们必须遍历所有存储桶及其中的所有元素),并且我们对所有数字执行此操作给出O(d * (n + b))的运行时间。

This is only linear if d is a constant and b is not asymptotically larger than n . 如果d是常数并且b不是渐近地大于n则这是线性的。 So indeed, if you have numbers of log n bits, it will take O(n log n) time. 实际上,如果你有log n位的数量,它将花费O(n log n)时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM