简体   繁体   English

在线性时间内对数组进行排序

[英]Sort an array in linear time

I'm stuck on this question: Given an array of n integers within the range of [0, 1, ... , n^5 - 1], how can you sort them in linear runtime?我被困在这个问题上:给定 [0, 1, ... , n^5 - 1] 范围内的 n 个整数数组,如何在线性运行时对它们进行排序? (O(n)) And more generally, if the range is [0, 1, ... , n^c - 1] (c is a natural constant bigger than 1) how would you do it? (O(n)) 更一般地说,如果范围是 [0, 1, ... , n^c - 1] (c 是一个大于 1 的自然常数),你会怎么做? Describe a proper algorithm and explain.描述一个正确的算法并解释。 My first thought was to convert the numbers to base n in both of the cases and then use radix sort (which uses counting sort as the sorting algorithm), but I've been told that I can't count on it that the conversion from decimal base to base n is O(1) So basically I'm pretty stuck as I have no idea I can I do it... Would be glad for help.我的第一个想法是在这两种情况下都将数字转换为基数 n,然后使用基数排序(使用计数排序作为排序算法),但有人告诉我我不能指望从以 n 为底数的十进制底数是 O(1) 所以基本上我很困惑,因为我不知道我能做到......很乐意提供帮助。

Hint: Bucket sort is a stable sort .提示:桶排序是一种稳定排序 So if you sort on condition A, then resort on condition B, you wind up sorted on B and then A.所以如果你按条件 A 排序,然后求助于条件 B,你最终会先按 B 排序,然后再按 A 排序。

Let's use // for integer division (dropping the remainder) and % for remainders.让我们使用//表示 integer 除法(去掉余数),使用%表示余数。 And now if you sort on x % m and then sort the output on (x // m) % m , you wind up with a list sorted on the last 2 digits in base b .现在,如果您按x % m排序,然后按(x // m) % m对 output 进行排序,您将得到一个按基数b的最后 2 位数字排序的列表。

Is that enough to get you going?这足以让你继续吗?

The term "integers" implies the values are stored as binary numbers, not decimal.术语“整数”意味着值存储为二进制数,而不是十进制数。 Since they're binary numbers use a base that is a power of 2, such as 256, which is common.由于它们是二进制数,因此使用的基数是 2 的幂,例如 256,这很常见。 Use shift and and instead of divide for fixed times.在固定时间使用shift and and而不是 divide。

For linear time complexity O(n), the code should sort based on the number of bits in an integer, typically 32 bits, so it would take 1 scan pass and 4 radix-sort passes.对于线性时间复杂度 O(n),代码应根据 integer 中的位数进行排序,通常为 32 位,因此需要 1 次扫描和 4 次基数排序。 Since the 1 and 4 are constants, time complexity is O(n).由于 1 和 4 是常量,时间复杂度是 O(n)。 If the sort is optimized to reduce the number of radix-sort passes based on range, although it is faster, it will have time complexity O(n log(range)).如果优化排序以减少基于范围的基数排序遍历次数,虽然速度更快,但时间复杂度为 O(n log(range))。

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

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