【问题标题】:Time complexity in sort function排序函数的时间复杂度
【发布时间】:2021-02-11 13:20:11
【问题描述】:

在 python 中,我们使用 sort 函数(例如,如果我们有一个 list-a,那么我们使用 a.sort() 对该列表进行排序)。但是如何知道排序函数使用的是哪种排序(冒泡、选择、插入等)。

【问题讨论】:

    标签: python sorting time-complexity dsa


    【解决方案1】:

    好吧,.sort python 函数使用Timsort 算法。您可以在这篇文章中了解更多信息 --> About Python's built in sort() method

    【讨论】:

    • 我正在解决一个预期时间复杂度为 O(N) 的问题,并且我使用了 .sort 函数。根据 Timsort,时间复杂度为 O(n logn)。因此,如果我使用 .sort 函数(因为它使用 Timsort 函数,时间复杂度为 nlogn )它不应该接受我的解决方案,但它确实接受了
    • @Soham 不,如果您在维基百科 timsort 页面的右侧看到,在最佳情况下,时间复杂度是 O(n) 而不是 O(n log n)。跨度>
    猜你喜欢
    • 1970-01-01
    • 2013-03-14
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    相关资源
    最近更新 更多