Python radixsort复杂度评估:为什么BIG_O推断Radixsort而不是O(B*N)的指数复杂度?
我已经实现了radixsort的数字列表(例如,与这个,唯一的区别是它使用了radixsort类,而IMO不应在术语上有任何区别复杂性):
class RadixSort:
def __init__(self):
self.base = 7
self.buckets = [[[] for i in range(10)] for i in range(self.base)] #list of sorting buckets, one bucket per digit
def sort(self, list1d):
"""
Sorts a given 1D-list using radixsort in ascending order
@param list1d to be sorted
@returns the sorted list as an 1D array
@raises ValueError if the list is None
"""
if list1d is None: raise ValueError('List mustn\'t be None')
if len(list1d) in [0, 1]: return list1d
for b in range(self.base): #for each bucket
for n in list1d: #for every number of the input list
digit = (n // (10 ** b)) % 10 #get last digit from the end for the first bucket (b=0), second last digit for second bucket (b=1) and so on
self.buckets[b][digit].append(n) #append the number to the corresponding sub-bucket based on the relevant digit
list1d = self.itertools_chain_from_iterable(self.buckets[b]) #merge all the sub-buckets to get the list of numbers sortd by the corresponding digit
return list1d
def itertools_chain_from_iterable(self,arr):
return list(chain.from_iterable(arr))
现在,我正在使用 o(b*n), b 是radixsort的基础,即要排序的数字中的最大数字数,即> len(self.buckets == self.base == b)和 n 是要排序列表中的数字数):
print('r.sort complexity:',big_o.big_o(r.sort,
lambda n: big_o.datagen.integers(n,1,9999999),
n_measures=10)[0])
输出如下:
r.sort complexity: Exponential: time = -2.9 * 0.0001^n (sec)
什么是我在这里做错了吗?我宁愿认为,关于我使用big_o的方式不正确,我的radixsort实现具有指数的复杂性。 任何提示/想法都将不胜感激!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论