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] = ' '
while getline(cin, str) read += str for i = 0 to read.size() if read[i] == '<' // process tag
while getline(cin, str) : O(1) read += str : O(1) for i = 0 to read.size() : O(1) if read[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 getline(cin, str) : O(1) read += str : O(1) for i = 0 to read.size() : O(1) if read[i] == '<' : O(1) // process tag : O(1) read[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 sequence T * data; unsigned next-data, max-data append(T e) if next-data >= max-data new-data = new [++max-data] T for i = 0 to next-data - 1 new-data[i] = data[i] delete [] data data = new-data data[next-data++] = e
sequence<int> is for i = 0 to 100 is.append(i)
if next-data >= max-data : O(1) new-data = new [++max-data] T : O(1)?/O(n) for i = 0 to next-data - 1 : O(1) new-data[i] = data[i] : O(1) delete [] data : O(1)?/O(n) 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<int> 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 sequence T * data unsigned next-data, max-data unsigned a = 50 append(T e) if next-data >= max-data new-data = new [max-data += a] T for i = 0 to next-data - 1 new-data[i] = data[i] delete [] data data = new-data data[next-data++] = e
if next-data >= max-data : O(1) new-data = new [max-data += a] T : O(n) for i = 0 to next-data - 1 : O(1) new-data[i] = data[i] : O(1) delete [] data : O(n) 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 sequence
T * data
unsigned next-data, max-data
append(T e)
if next-data >= max-data
new-data = new [max-data *= 2] T
for i = 0 to next-data - 1
new-data[i] = data[i]
delete [] data
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).
|