Tuesday, 11 May 2021

what is array

Array is one of the simplest and most  sophisticated data structures. In array data is stored at contiguous memory locations. E.g. say you have an array of 10 integers and array is placed at memory location 100. Now considering that each integer takes 4 bytes. 1st integer will be at 100, 2nd at 104, 3rd at 108 and so on....  

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. 

what is array

Array is one of the simplest and most  sophisticated data structures. In array data is stored at contiguous memory locations. E.g. say you h...