python学习笔记:算法复杂度

1. 算法复杂度:是指算法在编写为可执行程序后运行所需的资源,包括时间资源和内存资源,分别用时间复杂度空间复杂度描述。

2. big-O符号:(big O notation)代表“order of ...”(......阶),由德国数论学家Paul Bachmann于1892年著作中首先引入,其后由德国数论学家Edmund Landau推广,有时也称为朗道符号(Landau symbol)。描述函数的渐近行为,在CS中,在分析算法复杂度方面用处很大。

    3. 算法复杂度影响因素:

  • 算法采用的策略
  • 编译代码质量
  • 问题输入规模N
  • 机器执行指令速度

一、时间复杂度

(1)时间频度:记为T(n),对算法程序中语句执行次数的描述,也称为语句频度

(2)时间复杂度:记为O(n),对算法程序时间频度变化趋势的描述。当n趋近于无穷大时,若有某个辅助函数f(n),T(n)/f(n)为不等于0的常数,则T(n)=O(f(n)),这里f(n)称为T(n)的同数量级函数。

   1. 常见算法复杂度(从小到大,from http://www.krivers.net/15112-s18/notes/notes-efficiency.html

  • 常数:O(1)
  • 对数:O(logn)
  • 方根:O(n^0.5)
  • 线性:O(n)
  • 准线性:O(nlogn)
  • 平方:O(n^2)
  • 指数:O(k^n)
http://science.slc.edu/~jmarshall/courses/2002/spring/cs50/BigO/index.html

  2. python内置函数时间复杂度 (from http://www.krivers.net/15112-s18/notes/notes-efficiency.html

General
Function/MethodComplexityCode Example
PrintO(N)
# where N is the size of the input
print(input)
Range in IterationNumber of iterations = (end - start)/step
for i in range(start, end, step):...
Strings: s is a string with N characters
Function/MethodComplexityCode Example
LenO(1)
len(s)
MembershipO(N)
"a" in s
Get single characterO(1)
value = s[index]
Get sliceO(end - start)
s[start:end]
Get slice with stepO((end - start)/step)
s[start:end:step]
Chr() and Ord()O(1)
chr(s)
ord(s)
ConcatentationO(len(s1) + len(s2))
s3 = s1 + s2
Character Type MethodsO(N)
s.isalnum()
s.isalpha()
s.isdigit()
s.islower()
s.isspace()
s.isupper()
String Edit MethodsO(N)
s.lower()
s.upper()
s.replace()
s.strip()
Substring Search MethodsO(N)
s.count()
s.startswith()
s.endswith()
s.find()
s.index()
Lists: L is a list with N elements
Function/MethodComplexityCode Example
LenO(1)
len(L)
AppendO(1)
L.append(value)
ExtendO(K)
# len(a) = K
L.extend(a)
Concatentation with +=O(K)
# len(a) = K
L += a
Concatentation with +O(N + K)
# len(a) = K
L = L + a
Membership CheckO(N)
item in L
Pop Last ValueO(1)
L.pop()
Pop Intermediate ValueO(N)
L.pop(index)
Count values in listO(N)
L.count(item)
InsertO(N)
L.insert(index, value)
Get valueO(1)
value = L[index]
Set valueO(1)
L[index] = value
RemoveO(N)
L.remove(value)
Get sliceO(end - start)
L[start:end]
Get slice with stepO((end - start)/step)
L[start:end:step]
SortO(N log (N))
L.sort()
sorted(L)
MultiplyO(N*D)
#where D is an int
A = L*D
MinimumO(N)
min(L)
MaximumO(N)
max(L)
CopyO(N)
copy.copy(L)
Deep CopyO(N*M)
# where L is a 2D list with N rows and M cols
copy.deepcopy(L)
Sets: s is a set with N elements
Function/MethodComplexityCode Example
LenO(1)
len(s)
Membership

O(1)

原因在于:python中的set是基于哈希表实现的,这与dict一样,因此,计算elem的哈希函数获得地址,若地址存储的值=key,则查询成功,次过程不需要进行比较。

elem in s
Adding an ElementO(1)
s.add(elem)
Removing an ElementO(1)
s.remove(elem)
s.discard(elem)
UnionO(len(s) + len(t))
s|t
IntersectionO(min(len(s), len(t)))
s&t
DifferenceO(len(s))
s - t
ClearO(len(s))
s.clear()
CopyO(len(s))
s.copy()
Dictionaries: d is a dictionary with N key-value pairs
Function/MethodComplexityCode Example
LenO(1)
len(d)
MembershipO(1)
key in d
Get ItemO(1)
value = d[key]
d.get(key, defaultValue)
Set ItemO(1)
d[key] = value
Delete ItemO(1)
del d[key]
ClearO(N)
d.clear()
CopyO(N)
d.copy()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值