Advantages of array:
Since elements in array are placed at contiguous memory locations, complexity to access an element of array is O(1) e.g. say you want to access 7th element of an array, you don't need to traverse through first 6 elements. You can directly access 7th element.
Location of nth element in array = Start position of array + (n-1)*(size of element)
Disadvantages of array :
Since elements in array has to be in contiguous memory locations, it can't fit into fragmented memory. E.g. say you want to allocate 400 bytes(100 integers). There is empty memory of 400 bytes but it is not contiguous. 200 bytes are available at one location and other 200 bytes are at some other location. In this case despite having memory of 400 bytes, You will not be able to create array.
No comments:
Post a Comment