In Steven Skiena's The algorithm design manual Edition 2, he talks about dynamic arrays on page 67, which results in them having same Big-Oh as normal arrays. I don't understand how. This is what book states
Half the elements [in final array] were moved once, a quarter twice, and so on, so total movements M is given by
Isn't this counting some movements twice?
(The red part is copied from last array and blue is new inserted part)
For n
insertions there are lg n
array reallocations. So 1st item is copied lg n
times, second one lg n - 1
times, next 2 lg n - 2
times, next 4 lg n - 3
times and so on.
I don't know how to solve this but what am I thinking wrong?
Here's my latex code, if someone wants edit it here
M = 1 \cdot lg_2 n + 1 \cdot (lg_2 n - 1) + 2 \cdot (lg_2 n - 2) + 4 \cdot (lg_2 n - 3)
= \sum_{i = 1}^{lg_2 n - 1} 2^i \cdot (lg_2 n - i)