loading...

Dynamic Arrays

jjb profile image JB Updated on ・1 min read

Resources

  1. Dynamic arrays overview
  2. Dynamic arrays overview and implementation
  3. Dynamic arrays implementation

Takeaways:

  • Dynamic Arrays resize when they reach capacity, usually by doubling.
  • Dynamic Arrays, for insertions, have a worse case Big O of O(n) (linear). But due to doubling, the amortized* time complexity of Dynamic Arrays is O(1) (constant).
  • A Dynamic Array's space complexity is at worst O(2n) (immediately after doubling), with O(n) being wasted space (the empty half of the array). Note: O(2n) in Big O is reduced to O(n) - therefore the space complexity of a Dynamic Array is O(n).

*Another way of saying average. Per Wikipedia:

amortized analysis considers both the costly and less costly operations together over the whole series of operations of the algorithm

Per Brilliant:

Often, a data structure has one particularly costly operation, but it doesn't get performed very often. That data structure shouldn't be labeled a costly structure just because that one operation, that is seldom performed, is costly.

So, amortized analysis is used to average out the costly operations in the worst case.

Here's the finished implementation with test code:

If you found any errors in this post, please let me know!

Discussion

pic
Editor guide
Collapse
kesavanpalani profile image
Kesavan Palani

Good tutorial JB. Looking forward to follow your posts. One point I would like to add though.

The operation Prepend can be done by calling AddAt(element, 0) method inside the Prepend method :)

Collapse
jjb profile image
JB Author

Good catch, you are probably right!