Python radixsort复杂度评估:为什么BIG_O推断Radixsort而不是O(B*N)的指数复杂度?

发布于 2025-02-05 00:26:22 字数 2105 浏览 3 评论 0 原文

我已经实现了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实现具有指数的复杂性。 任何提示/想法都将不胜感激!

I have implemented RadixSort for a list of numbers (quite a similar implementation as e.g. this one, with the only difference that it uses a class RadixSort, which IMO shouldn't make any difference in terms of complexity):

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))

Now, I'm assessing its complexity using the big_o module (knowing that the complexity should actually be O(b*n) with b being the base of the RadixSort, i.e. the max number of digits in the numbers to be sorted, i.e. len(self.buckets == self.base ==b) and n being the number of numbers in the list to be sorted):

print('r.sort complexity:',big_o.big_o(r.sort,
                           lambda n: big_o.datagen.integers(n,1,9999999),
                           n_measures=10)[0])

And the output is as follows:

r.sort complexity: Exponential: time = -2.9 * 0.0001^n (sec)

What am I doing wrong here? I rather tend to think that something about the way I'm using big_o is not correct than that my RadixSort implementation has exponential complexity.
Any tips/ideas would be greatly appreciated!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文