StringBuilder read while str = input.readline() read.append(str) for i = 0 to read.length() if read.charAt(i) == '<' // process tag
StringBuilder read while str = input.readline() read.append(str) for i = 0 to read.length() if read.charAt(i) == '<' // process tag read.setCharAt(i, ' ')
while str = input.readline() read.append(str) for i = 0 to read.length() if read.charAt(i) == '<' // process tag
while str = input.readline() : O(1) read.append(str) : O(1) for i = 0 to read.length() : O(1) if read.charAt(i) == '<' : O(1) // process tag : O(1)
while O(1) : O(n) O(1) for O(1) if O(1) : O(1) O(1)
O(n) for O(1) : O(1) O(1)
O(n) O(n)
while str = input.readline() : O(1) read.append(str) : O(1) for i = 0 to read.length() : O(1) if read.charAt(i) == '<' : O(1) // process tag : O(1) read.setCharAt(i, ' ') : O(1)
while O(1) O(1) for O(1) if O(1) O(1) O(1)
|
while O(1) O(1) for O(1) O(1)
|
while O(1) O(1) O(n)
|
while O(1) : O(n) O(n)
|
class sequenceObject data [] int next-data append(T e) if next-data ≥ data.length() Object new-data [] = new Object [data.length() + 1] for i = 0 to data.length() - 1 new-data[i] = data[i] data = new-data data[next-data++] = e
sequence<Integer> is for i = 0 to 100 is.append(i)
if next-data ≥ data.length() : O(1) Object new-data [] = new Object [data.length() + 1] : O(1)?/O(n) for i = 0 to data.length() - 1 : O(1) new-data[i] = data[i] : O(1) data = new-data : O(1) data[next-data++] = e : O(1) if O(1) O(1) or O(n) for O(1) : O(n) O(1) O(1) or O(n) O(1)
if O(1) O(1) or O(n) O(n) O(1) or O(n) O(1)
|
if O(1) O(n) O(1)
|
O(n) O(1)
|
O(n)
|
sequence<Integer> is for i = 0 to 100 is.append(i)
How much work is going on here, worst case?
for i = 0 to n : O(1) is.append(i) : O(n)
append()
does O(n) work in the worst case.
for O(1) : O(n) O(n)
for i = 0 to n is.append(i)
append()
does work proportional to i
.
append()
is proportional to
1 + 2 + ... + n = n(n + 1)/2 = O(n2)
append()
have better behavior? How?
class sequenceObject data [] int next-data unsigned a = 50 append(T e) if next-data ≥ data.length() Object new-data [] = new Object [data.length() + a] for i = 0 to data.length() - 1 new-data[i] = data[i] data = new-data data[next-data++] = e
if next-data ≥ data.length() : O(1) Object new-data [] = new Object [data.length() + a] : O(n) for i = 0 to data.length() - 1 : O(1) new-data[i] = data[i] : O(1) data = new-data : O(1) data[next-data++] = e : O(1)
for i = 0 to n is.append(i)
with amortizing sequences.
append()
still does O(n) work on each
iteration, worst case.
for i = 0 to n is.append(i)
append()
does no work, or work proportional to
i
.
i
= ja, append()
does work proportional to ja.
append()
does work proportional to
0 + ... + a + ... + 2a + ... + ja = na(1 + 2 + ... + j) = aO(j) = O(j) = O(n)
|
|
append()
have asymptotically better behavior? How?
class sequenceObject data [] int next-data append(T e) if next-data ≥ data.length() Object new-data [] = new Object [data.length()*2] for i = 0 to data.length() - 1 new-data[i] = data[i] data = new-data data[next-data++] = e
for i = 0 to 100 is.append(i)
append()
does O(n) work in each iteration.
i
= 2j, append()
does work proportional to
2j, j a positive integer.
append()
does work proportional to
0 + 20 + 21 + 0 + 22 + ... + 2j, 2j = n
20 + 21 + ... + 2j = 2j + 1 - 1 = 2*2j - 1 = 2n - 1 = O(n).