0% found this document useful (0 votes)
68 views20 pages

Lecture-07-09 (Array Operations-Traversing-Insertion-Deletion-Update)

The document discusses various operations on array data structures including traversing, insertion, deletion, and updating. It provides algorithms and examples for each operation. Key aspects covered are traversing each element once, shifting elements to insert or delete, and replacing an element during update.

Uploaded by

zainakbardaudani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views20 pages

Lecture-07-09 (Array Operations-Traversing-Insertion-Deletion-Update)

The document discusses various operations on array data structures including traversing, insertion, deletion, and updating. It provides algorithms and examples for each operation. Key aspects covered are traversing each element once, shifting elements to insert or delete, and replacing an element during update.

Uploaded by

zainakbardaudani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Lecture: 07-09

Data Structures and Algorithm Analysis


Batch: 18CS, Semester: Second, Year: Second

Array Operations
(Traversing, Insertion, Deletion and Update Operation)

Presented by: Dr. Sammer Zai


Assistant Professor
Department of Computer Systems, Mehran, UET.

1
Fair Use Notice

The material used in this presentation i.e., pictures/graphs/text, etc.


is solely intended for educational/teaching purpose, offered free of
cost to the students for use under special circumstances of Online
Education due to COVID-19 Lockdown situation and may include
copyrighted material - the use of which may not have been
specifically authorized by Copyright Owners. It’s application
constitutes Fair Use of any such copyrighted material as provided in
globally accepted law of many countries. The contents of
presentations are intended only for the attendees of the class being
conducted by the presenter.
Contents

• Operations on Array Data Structure


• Traversing
• Insertion
• Deletion
• Update

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.

1. [initialize counter] Set K := LB


2. Repeat Steps 3 and 4 while K<= UB
3. [visit the element] Apply process to LA[k]
4. [increase counter] set K := K + 1
[End of Step-2 Loop]
6. Exit

6
Insertion into Array
 Insertion:

It is used to add a new data item in the given collection of data items.

E.g.We have linear array A as below:

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);

Enter data item that you want to insert? 100


Enter the location at which you want to insert your data? 2
Array before insertion is : 22,33,44,55,66
Array Values after insertion are: 22,33,100,44,55,66.

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

Consider LA is a linear array with N elements and K is a positive integer


such that K<=N. Following is the algorithm to delete an element
available at the Kth position of LA.

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

Consider LA is a linear array with N elements and K is a positive


integer such that K<=N. Following is the algorithm to update an
element available at the Kth position of LA.

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

You might also like