TEchMyEducation

easy tech learning...

array?advantages and disadvantages

No comments

Array

Array is a linear data structure that can store data in a sequential manner means array can store similar types of elements in a contiguous memory location. 
techmyeducation.blogspot.com_arrayadvantagesdisadvantages.jpg

Advantages of using Array:
  1. It consists of similar data type elements.
  2. Random access i.e we can access any value from an array based on the index.
  3. It is suitable when the number of elements is already known.
  4. Array is used in the implementation of other data structure i.e stack & queues.
Limitations(Disadvantages) of using Array:
  1. Static Memory Allocation:- One of the major disadvantages of the array is static memory allocation which means, During the compile time itself we have to specify the size of the array and there is no possibility to change the size of array during run time.
a[10] - If the requirement changes there is no possibility to increase the size during runt time of array which is initialized for 10 elements.

a[100] - If only 10 elements are used in array which is initialized for 100 elements then the memory for 90 elements is wasted.

       2. Insertion & Deletion:- Inserting and deleting an element in array is expensive means it requires more time complexity.
  • For Insertion:
current array
                     a[0]    a[1]   a[2]   a[3]    a[4]   a[5] 
                       10       20     30      40      50
Insert 5 at a[0]
                     a[0]    a[1]   a[2]   a[3]    a[4]   a[5] 
                       10       20     30      40      50
Now first the values move sequentially right  to insert 5 at a[0]

                     a[0]  ==>  a[1] ==>  a[2]  ==> a[3] ==>  a[4] ==> a[5] 
                                        10             20            30            40           50
And now it inserts 5 at a[0]
                      a[0]    a[1]   a[2]   a[3]    a[4]   a[5] 
                         0        10     20      30      40      50
The whole process takes more time so the main disadvantage of using array is in time complexity.

  • For Deletion:
current array
                     a[0]    a[1]   a[2]   a[3]    a[4]
                       10       20     30      40      50

Delete 10 from a[0]
                                a[0]    a[1]   a[2]   a[3]    a[4]
                                  10       20     30      40      50
Now the values move sequentially to left

                                a[0] <==  a[1] <==  a[2] <==  a[3] <==  a[4]
                                  20            30            40             50

If we are removing 10 from the location a[0] in that case array is moving every previous value sequentially to left.
So 20 will be moved to a[0] ,30 to a[1] .....

In Realtime here we are not removing the memory space, the memory space occupied by the 4th index is waste. We are only moving the element this is the main drawback of using array.

I hope now you all understand the concept of an array and all your doubts regarding array is cleared now. If you guys have still any doubts regarding array so you can comment on them via comment section and we are pleased to answer all your queries.

No comments :

Post a Comment