Ejemplo n.º 1
0
    N = 5
    a = [2, 5, 3, 1, 4]

    # 从小到大排序
    a.sort()
    print(a)

    # 从大到小排序
    a.sort(reverse=True)
    print(a)

    # 对字符串排序
    b = ["www", "algorithm", "racer", "text", "wait"]
    b.sort()
    print(b)
    b.sort(reverse=True)
    print(b)

    # 对向量排序
    v = Vector(["www", "algorithm", "racer", "text", "wait"])
    # 从小到大排序
    v.sort()
    print(v)
    # 从大到小排序
    v.sort(reverse=True)
    print(v)
    # 基于范围循环打印v中的元素
    for x in v:
        print(x, end=" ")

if __name__ == '__main__':
    N = 10000000

    v = Vector(N)

    # 线性时间
    start = time.process_time()
    length = v.size()
    for i, item in enumerate(v):
        v[i] = length - i
    end = time.process_time()
    print("运行时间(s):", end - start)

    # 线对时间
    start = time.process_time()
    v.sort()
    end = time.process_time()
    print("运行时间(s):", end - start)

    # 对数时间
    start = time.process_time()
    bisect.bisect(v, 1)
    end = time.process_time()
    print("运行时间(s):", end - start)

    # 平方时间,无法使用N这个规模量
    M = 10000
    start = time.process_time()
    for i in range(M):
        for j in range(M):
            v[i] *= j