计数排序具有时间复杂度: O(n+k) n->输入k-> abs的大小。迪夫。b/w最小值和最大值。在某些情况下,计数排序优于基于比较的排序算法,它是否存在于std: STL中的排序?如果没有,有什么理由吗?还有任何在STL中可用的计数排序的功能吗?
发布于 2020-04-26 07:39:27
计数排序是否存在于std中:在STL中进行排序?
不,它不存在。sort函数基于快速排序和其他启发式方法。
如果没有,有什么理由吗?
可能是因为它很容易实现。检查psuedocode
count = array of k+1 zeros
for x in input do
count[key(x)] += 1
total = 0
for i in 0, 1, ... k do
count[i], total = total, count[i] + total
output = array of the same length as input
for x in input do
output[count[key(x)]] = x
count[key(x)] += 1
return output还有任何在STL中可用的计数排序的功能吗?
C++ STL提供了基本的拟合数据结构-数组和向量,可用于实现计数排序。
map也可以用来实现计数排序,但是它不再是O(n)了,因为映射操作是O(logn)复杂性。
https://stackoverflow.com/questions/50199307
复制相似问题