gpt4 book ai didi

algorithm - 当整数来自 [1,100] 范围时,对 100 万个整数进行排序的最快方法是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:16:59 25 4
gpt4 key购买 nike

备注:我想过基数排序、桶排序、计数排序。

有没有办法实现大 O(n)?

最佳答案

您可以使用 counting sort .

Counting sort (sometimes referred to as ultra sort or math sort) is a sorting algorithm which (like bucket sort) takes advantage of knowing the range of the numbers in the array to be sorted (array A).

Counting sort is a stable sort and has a running time of Θ(n+k), where n and k are the lengths of the arrays A (the input array) and C (the counting array), respectively. In order for this algorithm to be efficient, k must not be much larger than n.

在这种情况下,k 是 100,n 是 1000000。

关于algorithm - 当整数来自 [1,100] 范围时,对 100 万个整数进行排序的最快方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3311053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com