python-3.x - 为什么我的蛮力(O((n1 + n2)log(n1 + n2)))解决方案比优化解决方案(O(n1 + n2))更快?

标签 python-3.x algorithm sorting data-structures time-complexity

我正在合并 2 个长度为 n1 和 n2 的排序数组,以返回长度为 n1+n2 的排序数组。

我的暴力解决方案的时间复杂度应该为 O((n1+n2)log(n1+n2)),而我的优化解决方案的时间复杂度应该为 O(n1+n2)。有人可以解释一下我的暴力破解方法是如何比优化解决方案快几乎 10 倍的吗?请找到下面的代码:

from time import time
import random

array1 = sorted([random.randint(1, 500) for i in range(100000)])
array2 = sorted([random.randint(1, 1000000) for i in range(100000)])

def brute_force(arr1, arr2):
    '''
    Brute force solution O((n1+n2)log(n1+n2)),  Space complexity O(1)

    '''
    #sorted function has the time complexity on nlog(n)
    return sorted(arr1+arr2)


def optimized_soln(arr1, arr2):
    '''
    More optimized soltuion Time Complexity O(n1+n2),  Space complexity O(n1+n2) 

    '''
    i = 0
    j = 0
    merged_array = []
    s1 = time()
    #if one of the arrays is empty , no need to merge
    if len(arr1) == 0:
        return arr2

    if len(arr2) ==0:
        return arr1

    while i<len(arr1):
        if arr1[i] <= arr2[j]:
            if i < (len(arr1) - 1):
                merged_array.append(arr1[i])
                i = i + 1
            else:
                merged_array.append(arr1[i])
                merged_array = merged_array + arr2[j:]
                i = len(arr1)
        else:
            if j < (len(arr2) - 1):
                merged_array.append(arr2[j])
                j = j + 1
            else:
                merged_array.append(arr2[j])
                merged_array = merged_array + arr1[i:]
                i = len(arr1)

    return merged_array


s1 = time()
print('Brute Force: O((n1+n2)log(n1+n2)),  Space complexity O(1)')
brute_force(array1,array2)
print('Time Taken: ',(time()-s1))
s2 = time()
print('Optimized Soln: Time Complexity O(n1+n2),  Space complexity O(n1+n2)')
optimized_soln(array1,array2)
print('Time Taken: ',(time()-s2))

最佳答案

您的强力解决方案在 Python 中以线性时间运行。一般来说,sort 的时间复杂度为 O(n log n),但这并不妨碍它在某些情况下更快。 Python 使用 timsort,它旨在利用输入数据中已排序的运行。

来自https://en.wikipedia.org/wiki/Timsort

Timsort was designed to take advantage of runs of consecutive ordered elements that already exist in most real-world data, natural runs. It iterates over the data collecting elements into runs and simultaneously putting those runs in a stack. Whenever the runs on the top of the stack match a merge criterion, they are merged.

因此,当您附加两个排序列表,然后使用 timsort 对它们进行排序时,它会找到两个范围,合并它们,然后停止。鉴于复杂性都是线性的,加速只是因为 sort 是用优化的 C 语言编写的,它很容易比直接用 Python 编写的代码快 10 倍或更多。

关于python-3.x - 为什么我的蛮力(O((n1 + n2)log(n1 + n2)))解决方案比优化解决方案(O(n1 + n2))更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65734541/

相关文章:

java - 在元素重置之前找到它曾经是什么数字

java - 使用两个不同的标准对数组进行排序

algorithm - 比较高度平衡树和重量平衡树

python - Hadoop 和 Python : Disable Sorting

python - 如何使用 pyspark 中等效的 pandas 轴删除列而不是行?

python - 用换行符将字典写入文本文件

python - 如何为可使用任意数量的 Any 类型的关键字参数调用的类型定义 Python 协议(protocol)?

python - 在 Python 中使用搜索算法解决三狼三羊之谜

python - re.sub 在标点符号和以标点符号开头或结尾的单词之间放置空格

python - 查找由具有 x、y、w、h 像素坐标的矩形覆盖的图 block 的坐标