Data Structures and Algorithm--数据结构与算法 1
Time complexity 时间复杂度
constant < logarithmic < linear < n-log-n < n ^ 2 < 2 ^ n
常数 < 对数 < 线性 < N-log-N < 平方 < 指数
O
(
1
)
<
O
(
l
o
g
n
)
<
O
(
n
)
<
O
(
n
l
o
g
n
)
<
O
(
n
2
)
<
O
(
2
n
)
O(1) < O(logn) < O(n) < O(nlogn) < O(n^2) < O(2^n)
O(1)<O(logn)<O(n)<O(nlogn)<O(n2)<O(2n)
Computing Prefix Averages(前缀平均值)
A[i] = (X[0] + X[1] + X[2] + … + X[i]) / (i+1)
计算数组A 的前 i 项的平均值
Quadratic
Algorithm PrefixAverage(x,n) // operation
input array x of n integers
output array a of prefix average of x
a = new average of n integres // n
// loop n *n = n^2
for (i = 0; i < n; i++){ // n
s = x[0];
for (j = 1; j < i; j++) // n
s = s + x[j];
a[i] = s / (i + 1);
return a;
}
Linear
Algorithm PrefixAverage(x,n) // operation
input array x of n integers
output array a of prefix average of x
a = new average of n integres // n
s = 0
for (i = 0; i < n; i++)
s = s + x[i]
a[i] = s / (i + 1)
return a;