数据结构与算法--时间复杂度

猜数游戏

1-100,猜一个我心中的数字?
第一次猜多少? 50 
最多猜几次? 7次 2^7 = 128  
1-  1000 猜几次? 2^10 = 1024
1 - 1000000猜几次 2 ^20  > 1000000
100 - 1000 - 1000000 变化很大; 7 - 10 - 20 变化很小

import time
import random
import matplotlib.pyplot as plt
import math
%matplotlib inline

def random_list(l):
    return [[int( 1000*random.random()) for i in range(l*n)] for n  in range(1,20)]

O(1)-Constant (常数级别)

def squre(x):
    return x *x

squre(3)
def first(x):  # x is a list
    start = time.time()
    r = x[0]   			# 取列表起始元素
    t = time.time - start 
    return r, len(x), t


def midle(x):
    start = time.time()
    r = x[len(x)//2]  # 取列表中间元素
    t = time.time() - start
    return r, len(x), t


def last(x):
    start = time.time()
    r = x[-1]   	# 取列表最后一个元素
    t = time.time() - start
    return r, len(x), t
random_list = random_list(10000)
rst = [last(l) for l in random_list]
len(rst)
rst

image.png

x  = list(zip(*rst))[1]
y = list(zip(*rst))[2]
plt.plot(x,y)

image.png
随着次数的增加的增加,时间基本此平在0.00。

O(log)-logarithm(对数级别)

二分查找就是log

import bisect
def bs(nums,target): # 二分查找
    sorted(nums)
    start = time.time()
    i = bisect.bisect_left(nums,target)
    if i != len(nums) and nums[i] == target:
        t = time.time() - start
        return i, len(nums), t
    t = time.time() - start
    return -1, len(nums), t
random_lists  = random_list(1000)

rst = [bs(l,100) for l in random_lists]
len(rst)
rst

image.png

x  = list(zip(*rst))[1]
y = list(zip(*rst))[2]
plt.plot(x,y)

image.png
随着次数的增加的增加,时间基本也此平在0.00,****但是和O(1)还是有区别的。
**

O(n)- linear (线性级别)

def find_max(l):    # 找最大的数
    start = time.time()
    if l == None:
        return None
    mx = l[0]
    for n in l:
        if n > mx:
            mx = n
    t = time.time() - start
    return mx, len(l), t
random_lists = random_list(20000)
rst = [find_max(l) for l in random_lists]
len(rst)
rst

image.png

x  = list(zip(*rst))[1]
y = list(zip(*rst))[2]
plt.plot(x,y)

image.png


不规律是因为计算机实时的复核是不一样的。

O(nlgn)- linear logarithmic(线性对数级别)

def mysort(l):     # 排序
    start = time.time()
    l.sort()
    t = time.time() - start
    return l[0],  len(l), t
random_lists = random_list(20000)
rst = [mysort(l) for l in random_lists]
len(rst)
rst

image.png

x  = list(zip(*rst))[1]
y = list(zip(*rst))[2]
plt.plot(x,y)

image.png

O(n)- square (平方级别)

def  has_duplicate(l): # 查看数组中是否有重复的数字
    start = time.time()
    rst = False
    for i in range(len(l)):
        for j in  range(i+1, len(l)):
            if l[i] == l[j]:
                rst = True
    t = time.time() - start
    return rst, len(l), t
random_lists = random_list(100)
rst = [ has_duplicate(l) for l in random_lists]
len(rst)
rst

image.png

x  = list(zip(*rst))[1]
y = list(zip(*rst))[2]
plt.plot(x,y)

image.png

评估算法运行时间

  • a = 最快的基本操作所需的运行时间
  • b = 最慢的基本操作所需的运行时间
  • a(时间复杂度) <= T(n) <= b(时间复杂度)
  • 因此,运行时间T(n)在两个线性函数之间

Big - O 记法


image.png


分析处算法的最差情况:比如1-100 最好猜1次中,一般猜4-5次,最差猜7次中,研究4, 5,7更有意义。


image.png
核心就是:根据多项式快速求出时间复杂度
例如: 5(N2)-22NlogN + 5N + 2   --->  O(N2
**            3NlogN**** + 5N   --->  O(NlogN**** ****)
image.png**








猜你喜欢

转载自www.cnblogs.com/sinlearn/p/12890662.html