Comparison of Linked Lists With Arrays and Dynamic Arrays
Comparison of Linked Lists With Arrays and Dynamic Arrays
Comparison of Linked Lists With Arrays and Dynamic Arrays
Index
• An element of an array say “A[ i ]” is calculated using the following
formula:
Address of A [ i ] = Base_Address + Width * ( i– LB )
• Where,
WidthStorage Size of one element in the array (in byte)
i Subscript (or) Index of element whose address is to be
found
LB Lower Bound of subscript, if not specified assume 0 (zero)
• Example:
print A[3]
address of A[3]= 1100 + 4 * 3 = 1100 +12 = 1112
hence, print A[3] print A[1112] 44(output)
Insertion on Array- at front
Insert 99 at the front of the Array
Given array X
A 10 34 26 17 29 8 12 99
0 1 2 3 6 7
4 5
A 2
99 10 34 26 17 29 8 12
0 1 2 3 4 5
Now copy 99 to array index-0 6 7
In general, Time Complexity required to insert the element at
front of the array= O(n), if the array is not full
Insertion on Array- at end
Insert 99 at the end of the Array
Given array
X
A 10 34 26 17 29 8 12 99
0 1 2 3 4 5 6
7
Given array
X
A 10 34 26 17 29 8 12 99
0 1 2 3 6 7
4 5
1
A 10 34 99 26 17 29 8 12
2 0 1 2 3 4 5
Now copy 99 to array index-2 6 7
In general, Time Complexity required to insert the element in the
middle (at any position) of the array= O(n) , if array is not full
Deletion on Array- at front
Delete value 10 from the Array
Given array Value to be
Deleted
A 10 34 26 17 29 8 12 10
0 1 2 3 4 5 6 7
Remove value at index-0
A 34 26 17 29 8 12
0 1 2 3 4 5
6 7
Shift the array element to adjust the values to start from index-0
34 26 17 29 8 12
A
0 1 2 3 4
Ingeneral, Time Complexity required
5 6 to delete
7 the element at
front of the array= O(n)
Deletion on Array- at end
Delete value 12 from the Array
A 10 34 26 17 29 8
0 1 2 3 4 5 6 7
We know that address calculation from index position requires constant time
A 10 34 17 29 8 12
0 1 2 3 4 7
5 6
Shift the array 10
element34
to adjust
17 the 29
empty space
8 at12index-2
A
0 1 2 3 4 5
6 7
In general, Time Complexity required to delete the element in the
middle (at any position) of the array= O(n)
Advantages of Arrays
Simple and easy to use
Faster access to the elements (constant access)
Disadvantages of Arrays
Pre-allocates all needed memory and wastes memory space for
indices in the array that are empty.
Fixed size: The size of the array is static (specify the array size
before using it).
One block allocation (contiguous memory allocation): To allocate
the array itself at the beginning, sometimes it may not be possible
to get the memory for the complete array (if the array size is big).
Data must be shifted during insertions and deletions
Dynamic Arrays
A 2
99 10 34 26 17 29 8 12
0 1 2 3 4 5
Now copy 99 to array index-0 6 7
In general, Time Complexity required to insert the element at
front of the array= O(n), if the array is full or not full
Insertion on Dynamic Array- at end
Insert 99 at the end of the Array
Case-1 : array has space (array is not Full)
Given array X
A 10 34 26 17 29 8 12 99
0 1 2 3 4 5 6 7
A 10 34 26 17 29 8 12 99
0 1 2 3 4 5 6 7
!The given array is full and there is no space for a new element! So we will
create a new bigger array . Double the given array size
New array size= 14
B
0 1 2 3 4 5 6 7 10 11 12 13
8 9
Now copy the elements of the old array into new array
B 10 34 26 17 29 8 12
0 1 2 3 4 5 6 7 8 9 10 11 12 13
Now insert 99 to new array at index- 7
B 10 34 26 17 29 8 12 99
0 1 2 3 4 5 6 7 8 9 10 11 12 13
1
A 10 34 99 26 17 29 8 12
20 1 2 3 4 5
Now copy 99 to array index-2 6 7
In general, Time Complexity required to insert the element in the
middle (at any position) of the array= O(n), if array is full or not full
Deletion on Dynamic Array- at front
Delete value 10 from the Array
Given array Value to be
Deleted
A 10 34 26 17 29 8 12 10
0 1 2 3 4 5 6 7
Remove value at index-0
A 34 26 17 29 8 12
0 1 2 3 4 5
6 7
Shift the array element to adjust the values to start from index-0
34 26 17 29 8 12
A
0 1 2 3 4 5
Ingeneral, Time Complexity
6 required
7 to delete the element at
front of the array= O(n)
Deletion on Dynamic Array- at end
Delete value 12 from the Array
Value to be
Given array Deleted
A 10 34 26 17 29 8 12 12
0 1 3 4 5 6 7
2
We know that address calculation from index position requires constant time
A 10 34 17 29 8 12
0 1 2 3 4 7
5 6
Shift the array 10
element34
to adjust
17 the 29
empty space
8 at12index-2
A
0 1 2 3 4 5
6 7
In general, Time Complexity required to delete the element in the
middle (at any position) of the array= O(n)
• Advantages of Dynamic Array:
Fast random access of elements.
Efficient in terms of memory usage
Variable size. You can add as many items as you want,
and the dynamic array will expand to hold them.
Insertion at the end O(n) O(1), if array is not O(1), if array is not
full full
O(n), if array is full
Deletion at the end O(n) O(1) O(1)
Insertion in Middle O(n) O(n), if array is not O(n)
full (for shifting)
Deletion in Middle O(n) O(n) O(n)