使用 Python 进行线性搜索和二分搜索之间经过的时间

Time elapsed between linear search and binary search using Python

我在下面制作了两个 Python 函数,一个用于顺序(线性)搜索,另一个用于二进制搜索。

我想为给定列表中的每个尺寸值做这 3 件事:

  1. 为给定的列表大小生成一个随机整数值列表(1 到 1000,0000 之间)

  2. 运行顺序搜索列表中的-1并记录顺序搜索经过的时间

  3. 运行对排序后的列表(排序后的列表)进行二分查找-1,并记录二分查找所经过的时间

我所做的是:

def sequentialSearch(alist, item):
    pos = 0
    found = False

    while pos < len(alist) and not found:
        if alist[pos] == item:
            found = True
        else:
            pos = pos + 1

    return found

def binSearch(list, target):
    list.sort()
    return binSearchHelper(list, target, 0, len(list) - 1)

def binSearchHelper(list, target, left, right):
    if left > right:
        return False

    middle = (left + right)//2
    if list[middle] == target:
        return True
    elif list[middle] > target:
        return binSearchHelper(list, target, left, middle - 1)
    else:
        return binSearchHelper(list, target, middle + 1, right)

import random
import time
list_sizes = [10,100,1000,10000,100000,1000000]
for size in list_sizes:
    list = []
    for x in range(size):
        list.append(random.randint(1,10000000))

    sequential_search_start_time = time.time()
    sequentialSearch(list,-1)
    sequential_search_end_time = time.time()
    print("Time taken by linear search is = ",(sequential_search_end_time-sequential_search_start_time))

    binary_search_start_time = time.time()
    binSearch(list,-1)
    binary_search_end_time = time.time()
    print("Time taken by binary search is = ",(binary_search_end_time-binary_search_start_time))

    print("\n")

我得到的输出是:

众所周知,二分搜索比线性搜索快得多。 所以,我只想知道为什么它显示二分查找消耗的时间比线性查找消耗的时间多?

1) 您需要考虑排序时间。二分搜索仅适用于排序列表,因此排序需要时间,这需要它的时间复杂度 O(nlogn)。在您的情况下,您是在计时器启动后对其进行排序,因此它会更高。

2) 您正在搜索列表中不存在的元素,即 -1,这不是二进制搜索的一般情况。二分搜索的最坏情况是必须进行如此多的跳跃才能永远找不到该元素。

3) 请不要将 list 用作变量,它是 python 的关键字,您显然正在覆盖它。使用其他东西。

现在,如果您不对列表进行计时就对列表进行排序。结果发生巨大变化。这是我的。

Time taken by linear search is =  9.059906005859375e-06
Time taken by binary search is =  8.58306884765625e-06


Time taken by linear search is =  1.2159347534179688e-05
Time taken by binary search is =  4.5299530029296875e-06


Time taken by linear search is =  0.00011110305786132812
Time taken by binary search is =  5.9604644775390625e-06


Time taken by linear search is =  0.0011129379272460938
Time taken by binary search is =  8.344650268554688e-06


Time taken by linear search is =  0.011270761489868164
Time taken by binary search is =  1.5497207641601562e-05


Time taken by linear search is =  0.11133551597595215
Time taken by binary search is =  1.7642974853515625e-05

我所做的只是在计时之前对列表进行排序。方式,比您必须一次对它进行排序和搜索要好得多。