一、引例
先看一段伪代码:
procedure main(arg):
loop = arg
for i in 0 to n do:
statements 1
statements 2
next i
endprocedure
/*statements 1*/
procedure ProT(arg):
for j in 0 to arg do:
temp = j
next j
endprocedure
/*statements 2*/
procedure ProS():
loop = 1
endprocedure
现在计算它的时间复杂度。假设一条赋值语句的时间复杂度为1,则statements2的时间复杂度为1,statements1的时间复杂度为参数arg,在main中,时间复杂度为f = 1 + 1 + 2 + 3 + ... + n + n = 1/2 * n^2 + 3/2 * n + 1。
二、O(n)
f(n) = O(g(n)),表示当且仅当n>m时,|f(n)| <= c|g(n)|,c为常数。
<