Lecture-07-09 (Array Operations-Traversing-Insertion-Deletion-Update)
Lecture-07-09 (Array Operations-Traversing-Insertion-Deletion-Update)
Array Operations
(Traversing, Insertion, Deletion and Update Operation)
1
Fair Use Notice
3
Basic operations ofArrays
Some common operation performed on array are:
◦ Traversing
◦ Insertion
◦ Deletion
◦ Update
◦ Searching
◦ Sorting
◦ Merging
4
Traversing Arrays
◦ Traversing: It is used to access each data item exactly once so that it
can be processed.
◦ E.g. We have linear array A as below:
1 2 3 4 5
10 20 30 40 50
◦ Here we will start from beginning and will go till last element and
during this process we will access value of each element exactly once as
below:
◦ A [1] = 10
◦ A [2] = 20
◦ A [3] = 30
◦ A [4] = 40
◦ A [5] = 50 5
Algorithm for Traversing an Array
(Traversing a Linear Array) Here LA is a linear array with lower bound LB and
upper bound UP. This algorithm traverses LA applying an operation PROCESS
to each element of LA.
6
Insertion into Array
Insertion:
It is used to add a new data item in the given collection of data items.
1 2 3 4 5
10 20 50 30 15
New element to be inserted is 100 and location for insertion is 3. So shift the elements
from 5th location to 3rd location downwards by 1 place.And then insert 100 at 3rd
location. It is shown below:
7
Insertion into Array
8
Insertion into Array
9
Algorithm for Insertion In Arrays
Let LA be a Linear Array (unordered) with N elements and K is a positive
integer such that K<=N. Following is the algorithm where ITEM is inserted
into the Kth position of LA.
10
Example of Insertion Operation
Data is over-written
11
Exercise Problem
• Consider the insertion algorithm (Slide-10) along with the example of
insertion operation (Slide-11) and produce the following output using
C++ with different values of K(location at which new data has to be
inserted);
12
Deletion from Array
Deletion: It is used to delete an existing data item from the given collection
of data items.
13
Algorithm for Deletion from Arrays
14
Complexity of Insertion and Deletion
Algorithm
• Because it takes a single step to access an item of an array via its index, or
add/remove an item at the end of an array, the complexity for accessing, pushing
or popping a value in an array is O(1). Whereas, linearly searching through
an array via its index, as seen before, has a complexity of O(n).
• That would be considered an O(N) operation, because all of the other elements in
the array will need to be moved.
• For example, if my array had 1,000 elements in it - occupying indices 0–999 - and I
remove element 0, it would then have an empty slot at element 0, with
remaining elements at indices 1–999.
• That’s not a valid state for the array to be in, so to fill that empty slot, the array
data structure would need to move its remaining contents. So the content of
element 1 is moved to now occupy element 0, the content of element 2 is moved
to element 1, etc., until the array’s 999 remaining elements now occupy indices
0–998. Every single item remaining in the array had to be moved, hence O(N).
15
Complexity of Insertion and Deletion
Algorithm
• When inserting at the front of the array, the process is similar. To make space for the
element that will be added at index 0, the array data structure must first move the
existing contents over to free up slot 0, so it can store the new element there. So again, a
O(N) operation.
• In fact, because the worst-case scenario is O(N), even if you hadn’t specified that you’re
inserting at the front specifically, the insert and delete operations in an array data
structure are always considered to have O(N) complexity.
• However, if you were to provide the restriction that you would only add/remove from
the back of the array rather than the front - eliminating the need to shift the position of
other elements - and you instructed the array to reserve enough memory in its internal
storage up-front such that reallocations of that internal storage would not be necessary,
then those add/remove operations would be O(1) instead of O(N), and you would have
effectively created the data structure known as a “stack”.
• Since accessing an element of the array only requires a calculation of indexes, its
complexity is O(1).
16
Update Operation
1. Start
2. Set LA[K] := ITEM
3. Stop
17
Example of Update Operation
• The original array elements are:
• LA[0] = 1
• LA[1] = 3
• LA[2] = 5
• LA[3] = 7
• LA[4] = 8
• The array elements after updating:
• LA[0] = 1
• LA[1] = 3
• LA[2] = 10
• LA[3] = 7
• LA[4] = 8
18
Exercise Problem
1. State your point of view regarding what happens when an element is
inserted at the end of a linear array and when we need to insert an
element in the middle of an array?
2. Explain the best, worst, and average case scenarios for deletion
operation?
3. Write down an algorithm and a C++ code to perform update operation.
The program should initialize the original array with the following values:
{21,32,43,54,12,31,65,76,89,3,5,7,8,12,48,7,6}. Update the records saved
at locations 6 and 9 in the original array by 100 and 200 respectively.
Display the updated array elements.
19
Output of 3rd Question
20