string read while getline(cin, str) read += str for i = 0 to read.size() if read[i] == '<' // process tag
string read while getline(cin, str) read += str for i = 0 to read.size() if read[i] == '<' // process tag read[i] = ' '
How many times is the loop body executed if the input contains 17 items?
Constant O(1) array index Log O(log n) binary search Linear O(n) linear search Log-linear O(n log n) heapsort Quadratic, square O(n2) bubble sort
|
|
f f(n) C C log n 6.6 n 100 n log n 664.4 n2 10,000
- Improving asymptotic behavior is important.
- Move to the next lowest level of asymptotic behavior.
i = 0 while i < n j = i + 1 while j < n if a[i] > a[h] swap(a[i], a[j]) j++ i++
i = 0 while i < n j = i + 1 while j < n if a[i] > a[h] swap(a[i], a[j]) j++ i++
|
O(1) while i < n O(1) while j < n if a[i] > a[h] O(1) O(1) O(1)
|
if
statementif b s1 else s2
b
's cost.
if
Estimates
O(1) while i < n O(1) while j < n if a[i] > a[h] O(1) O(1) O(1)
|
O(1)
while i < n
O(1)
while j < n
O(1) + O(1) = O(1)
O(1)
O(1)
|
;
s2
|
|
O(1) while i < n O(1) while j < n O(1) O(1) O(1)
|
O(1) while i < n O(1) while j < n O(1) + O(1) = O(max(1, 1)) = O(1) O(1)
|
while b do s
s
has asymptotic estimate O(f).
s
gets executed some number of times.
b
.
O(1) while i < n O(1) while j < n O(1) O(1)
|
O(1) while i < n O(1) O(n)*(O(1) + O(1)) = O(n)*O(max(1, 1)) = O(n)*O(1) = O(n*1) = O(n) O(1)
|
O(1) while i < n O(1) O(n) O(1)
|
O(1) while i < n O(1) + O(n) + O(1) = O(max(1, n, 1) = O(n)
|
O(1) while i < n O(n)
|
O(1) O(n)*O(n) = O(n*n) = O(n2)
|
O(1) O(n2)
|
O(1) + O(n2) = O(max(1, n2)) = O(n2)
|
swap(a, b)
= O(1).
f(n) ≤ Cg(n)for all n > N.