线性时间排序?

发布于 2024-07-17 06:12:35 字数 1571 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

神回复 2024-07-24 06:12:35

还要看看相关的排序:pigeonhole sort计数排序,以及基数排序 正如 Pukku 提到的。

Also take a look at related sorts too: pigeonhole sort or counting sort, as well as radix sort as mentioned by Pukku.

酷到爆炸 2024-07-24 06:12:35

查看基数排序

Have a look at radix sort.

情绪少女 2024-07-24 06:12:35

当人们说“排序算法”时,他们通常指的是“比较排序算法”,这是任何仅依赖于能够询问“这个东西比那个更大还是更小”的算法。 因此,如果您仅限于询问有关数据的这一问题,那么您将永远不会得到超过 n*log(n) (这是对数据集的 n 阶乘可能排序进行 log(n) 搜索的结果) 。

如果您可以摆脱“比较排序”的限制并询问有关一段数据的更复杂的问题,例如“该数据的以 10 为底的基数是多少”,那么您可以想出任意数量的线性时间排序算法,它们只是占用更多内存。

这是一个时间空间的权衡。 比较排序需要很少的内存或不需要内存,并且运行时间为 N*log(n) 。 基数排序(例如)在 O(n) 时间和 O(log(radix)) 内存中运行。

When people say "sorting algorithm" they often are referring to "comparison sorting algorithm", which is any algorithm that only depends on being able to ask "is this thing bigger or smaller than that". So if you are limited to asking this one question about the data then you will never get more than n*log(n) (this is the result of doing a log(n) search of the n factorial possible orderings of a data set).

If you can escape the constraints of "comparison sort" and ask a more sophisticated question about a piece of data, for instance "what is the base 10 radix of this data" then you can come up with any number of linear time sorting algorithms, they just take more memory.

This is a time space trade off. Comparason sort takes little or no ram and runs in N*log(n) time. radix sort (for example) runs in O(n) time AND O(log(radix)) memory.

一梦浮鱼 2024-07-24 06:12:35

维基百科展示了许多不同的排序算法及其复杂性。 你可能想看看

wikipedia shows quite many different sorting algorithms and their complexities. you might want to check them out

琉璃梦幻 2024-07-24 06:12:35

这非常简单,如果 n=2 并且数字是唯一的:

  • 构造一个位数组(2^31-1 位 => ~256MB)。 将它们初始化为零。
  • 读取输入,对于您看到的每个值,将数组中的相应位设置为 1。
  • 扫描数组,对于每个设置的位,输出相应的值。

复杂性=> O(2n)

否则,使用基数排序:

复杂度 => O(kn)(希望如此)

It's really simple, if n=2 and numbers are unique:

  • Construct an array of bits (2^31-1 bits => ~256MB). Initialize them to zero.
  • Read the input, for each value you see set the respective bit in the array to 1.
  • Scan the array, for each bit set, output the respective value.

Complexity => O(2n)

Otherwise, use Radix Sort:

Complexity => O(kn) (hopefully)

这个俗人 2024-07-24 06:12:35

将这些数字视为三位数字,其中每个数字的范围从 0 到 n-1。 使用基数排序对这些数字进行排序。 对于每个数字,都会调用计数排序,该排序需要花费 Theta(n+n) 时间,因此总运行时间对应于 Theta(n)。

Think the numbers as three digit numbers where each digit ranges from 0 to n-1. Sort these numbers with radix sort. For each digit there is a call to counting sort which is taking Theta(n+n) time, so that the total running time corresponds to Theta(n).

蝶舞 2024-07-24 06:12:35

一组有限范围的数字可以由 RANGE 位的位图表示。
在本例中,位图大小为 500mb,因此对于除大型列表之外的任何内容,最好使用基数排序。 当遇到数字 k 时,设置 bitmap[k] = 1。单次遍历列表,O(N)。

A Set of a limited range of numbers can be represented by a bitmap of RANGE bits.
In this case, a 500mb bitmap, so for anything but huge lists, you'd be better off with Radix Sort. As you encounter the number k, set bitmap[k] = 1. Single traversal through list, O(N).

以酷 2024-07-24 06:12:35

类似的算法是可能的:

M;// unsorted array
lngth; //number items of M
for(int i=0; i < lngth; i++)sorted[M[i]];

它是线性复杂度的唯一可能的算法,但它的复杂度为 O(k*N) by ram(N - 数组元素数,k - 元素的 len)

alike algo is possible:

M;// unsorted array
lngth; //number items of M
for(int i=0; i < lngth; i++)sorted[M[i]];

It's alone possible algo for linear complexity, but it has complexity O(k*N) by ram (N - number array elements, k -- element's len)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文