Tree-based data structures, such as a rope or finger tree , can often provide logarithmic insertion times at arbitrary positions. Compromise is access time, which is also logarithmic, except in special cases, such as fingertips.
Dynamic arrays can provide amortized constant insertion at the ends, but insertion in the middle requires copying part of the array and is O (N) in time, as you mentioned.
It is possible to implement a data structure that supports a depreciated constant average insert. If you add to either end, consider it as a dynamic array. If pasted in the middle, save the old array and add a new array βaboveβ that contains the new βmiddleβ list, using the old array for the data that is to the left or right of the middle. The access time will be logarithmic after the first average insertion and will keep track of which data was in which layer quickly becoming complicated.
It may be the "tiered" dynamic array mentioned in the wikipedia article, I have not explored it yet.
I suspect that no one really uses a data structure such as the fact that the middle insert is rarely found when you most need it, and the logarithmic insert (using trees) is good enough for most real-world cases.
source share