An array is a collection of elements of the same data type stored in contiguous memory cells. It has a fixed size and is by default passed by reference to a function.
Eg. int arr[6] = {4, -3, 8, 5, -1, 6};
It initializes an integer array 'arr' storing 6 elements.
Assuming the size of an integer to be 4 bytes and base cell address to be 2000. It can be represented as -
A dynamic array is similar to a static array but it has the ability to automatically resize itself when an element is inserted or deleted.
They are available as vectors in C++ and likewise lists in Java.
Eg. list<int> l;
vector<int> v;
Vectors are slightly less efficient than static arrays due to the occasional resizing and copying of elements.
The amortized time complexity of insertion in a dynamic array is O(1).
[Amortized Time complexity = No. of operations / No. of pushbacks]
Comments
Post a Comment