0% found this document useful (0 votes)
21 views

Topic 2a-Array and Its Operations (Insertion and Deletion)

The document discusses arrays and their operations like traversal, insertion, deletion and sorting. It explains what arrays are, how they are defined and indexed. It then describes the different array operations and provides algorithms to perform insertion and deletion at different positions in an array. Complexity analysis for different operations is also presented.

Uploaded by

razaaziz9191
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Topic 2a-Array and Its Operations (Insertion and Deletion)

The document discusses arrays and their operations like traversal, insertion, deletion and sorting. It explains what arrays are, how they are defined and indexed. It then describes the different array operations and provides algorithms to perform insertion and deletion at different positions in an array. Complexity analysis for different operations is also presented.

Uploaded by

razaaziz9191
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Topic2: Array and its Operations

-Insert
-Delete

1
Introduction to Array
⚫ Arrays are referred to as structured data
types. An array is defined as finite ordered
collection of homogenous data, stored in
contiguous memory locations.
⚫ The elements of array are accessed through
index set containing ‘n’ consecutive numbers.
◦ finite means data range must be defined.
◦ ordered means data must be stored in
continuous memory addresses.
◦ homogenous means data must be of similar data
type.

2
Introduction to Array
⚫ In C language , index of an array starts
from 0 so if there are ‘n’ values then ‘n-1’
indexes will be used.
⚫ Example:
Data: 2, 4, 6, 8 (n=4)

2 4 6 8
0 1 2 3
⚫ Length or size of above array is 4.

3
Operations supported by Array
⚫ Traversal
⚫ Insertion
⚫ Deletion
⚫ Sorting
⚫ Search

4
Traversal Operation in an Array
Variables:
START: Initialised with starting index of array.
N: Number of elements in array
A: Variable for array
2 4 6 8
Algorithm: A[0] A[1] A[2] A[3]

1. START = 0
2. Repeat Step 3 & 4 while (START<N)
3. Read A [START]
4. START = START + 1

5
Complexity of Traversal Operation
in an Array
Algorithm:
1. START = 0
2. Repeat Step3 & 4while (START<N)
3. Read A [START] 2 4 6 8
4. START = START + 1 A[0] A[1] A[2] A[3]
Complexity Analysis
Statement# Operations Iterations Sub-Total
T(n)=4N+2
1 1 1 1
O(N)
2 1 N+1 N+1
3 1 N N
4 2 N 2N

6
Summary
⚫ Big O notation gives the upper bound or worst case
scenario for a particular algorithm
⚫ If an algorithm has the single loop then big O notation
will be equal to the iterations of that loop
⚫ If an algorithm has nested loops then big O notation will
be equal to the number of iterations of outer loop
multiplied by the number of iterations of inner loop.
⚫ The big O notation of a constant will always be 1.
⚫ In case of consecutive statements, addition is performed
to find out the complexity.

7
Summary
⚫ If an algorithm has conditional statement then
complexity includes running time of conditional
statement along with the maximum running time of
nested code within if or else statement.
⚫ The big O notation for an algorithm dividing the
problem size by a constant factor will always be defined
in terms of Log n.
⚫ Array is the linear data structure in which data is stored
in continuous memory location and can be accessed
through indexes.

8
Insertion Operation in an Array
‘Insert’ operation means adding
one or more data elements into
an array. Insertion operation
does not overwrite any index
value.
Types of Insertions:
1. Insertion at the beginning of array
2. Insertion at the end of array
3. Insertion in the middle of array

9
Insertion Operation in an Array
Variables:
LA: Liner Array
N: Number of elements in LA
K: Position where insertion should be done
ITEM: An element that needs to be inserted
Algorithm:
1. Set J = N-1
2. Repeat steps 3 and 4 while J ≥ K
3. Set LA[J+1] = LA[J]
4. Set J = J-1
5. Set LA[K] = ITEM
6. Set N=N+1
7. Exit

10
Insertion Operation in an
Array- @ End
LA[0] LA[1] LA[2] LA[3]
Algorithm:
2 4 6 8
1. Set J = N-1
2. Repeat steps 3 and 4 while J ≥ K
3. Set LA[J+1] = LA[J]
4. Set J = J-1
5. Set LA[K] = ITEM
6. Set N=N+1
7. Exit Iteration-2
(N=4,K=3,ITEM=1)
Repeat while J(2)≥k(4)
Set LA[4]=7
Set N=5
Exit

2 4 6 8 7
LA[0] LA[1] LA[2] LA[3] LA[4]
11
Insertion Operation in an Array-@Beginning
LA[0] LA[1] LA[2] LA[3]
2 4 6 8
Algorithm: Iteration-3(N=4,K=0,IT
1. Set J = N-1 EM=1)
2. Repeat steps 3 and 4 while J ≥ K Repeat while J(1)≥k(0)
3. Set LA[J+1] = LA[J] LA[1+1]=LA[1], J=0
4. Set J = J-1
Iteration- 4
5. Set LA[K] = ITEM (N=4,K=0,ITEM=1)
6. Set N=N+1 Repeat while J(0)≥k(0)
7. Exit
LA[0+1]=LA[0], J=-1
Iteration- 1 Iteration-2
Iteration-5
(N=4,K=0,ITEM=1) (N=4,K=0,ITEM=1)
(N=4,K=0,ITEM=1)
J=3, repeat while J(3) Repeat while J(-1)≥k(0)
Repeat while J(2)≥k(0)
≥k(0)
Set LA[0]=1
LA[3+1]=LA[3], J=2 LA[2+1]=LA[2], J=1
N=5
1 2 4 6 8
LA[0] LA[1] LA[2] LA[3] LA[4]
12
Insertion Operation in an
Array-In middle
LA[0] LA[1] LA[2] LA[3]
Algorithm:
2 4 6 8
1. Set J = N-1
2. Repeat steps 3 and 4 while J ≥ K
3. Set LA[J+1] = LA[J]
4. Set J = J-1
5. Set LA[K] = ITEM
Iteration-3(N=4,K=2,IT
6. Set N=N+1 EM=5)
7. Exit Repeat while J(1)≥k(2)
Iteration-1(N=4,K=2,IT Set LA[2]=5
EM=5)
N=5
J=3, repeat while J(3)
≥k(2) 2 4 5 6 8
LA[3+1]=LA[3], J=2
Iteration-2(N=4,K=2,IT LA[0] LA[1] LA[2] LA[3] LA[4]
EM=5)
Repeat while J(2)≥k(2)
13
LA[2+1]=LA[2], J=1
Comparison of Insertion
Operation in an Array
LA[0] LA[1] LA[2] LA[3]
Algorithm:
2 4 6 8
1. Set J = N-1
2. Repeat steps 3 and 4 while J ≥ K
3. Set LA[J+1] = LA[J]
4. Set J = J-1
5. Set LA[K] = ITEM
6. Set N=N+1
7. Exit
Location of At the start At the end In the
N=4 insertion of array of array middle of
array
Number of 4 0[constant 2
Iterations time for
step 5 & 6]
Big O O(N) O(1) Average
Notation WORST Best
14
Deletion Operation in an Array
‘Delete’ operation means
removing one or more data
elements from an array.
Deletion operation reduces the
size of array.
Types of Deletions:
1. Deletion at the beginning of array
2. Deletion at the end of array
3. Deletion in the middle of array

15
Deletion Operation in an Array
Variables:
LA: Liner Array
N: Number of elements in LA
K: Position at which deletion should be done

Algorithm:
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N-1
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop

16
Deletion Operation in an
Array-From END
Algorithm: LA[0] LA[1] LA[2] LA[3]
1. Start
2 4 6 8
2. Set J = K
3. Repeat steps 4 and 5 while J < N-1
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop

Iteration-1(N=4, K=3) LA[0] LA[1] LA[2]


J=3, while J(3) <
N-1(3) 2 4 6
Set N=3

17
Deletion Operation in an
Array-From Beginning
Algorithm: LA[0] LA[1] LA[2] LA[3]
1. Start
2 4 6 8
2. Set J = K
3. Repeat steps 4 and 5 while J < N-1 Iteration-3(N=4, K=0)
4. Set LA[J] = LA[J + 1] J=2, while J(2) < N-1(3)
5. Set J = J+1 Set LA[2]=LA[3], Set
6. Set N = N-1 J=3
7. Stop
Iteration-1(N=4, K=0) Iteration-2(N=4, K=0) Iteration-4(N=4, K=0)
J=0, while J(0) < N-1(3) J=1, while J(1) < N-1(3) J=3, while J(3) <
N-1(3)
Set LA[0]=LA[1], Set Set LA[1]=LA[2], Set
J=1 J=2 Set N=3
4 6 8
LA[0] LA[1] LA[2]

18
Deletion Operation in an
Array-Middle
Algorithm: LA[0] LA[1] LA[2] LA[3]
1. Start
2 4 6 8
2. Set J = K
3. Repeat steps 4 and 5 while J < N-1
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop

Iteration-1(N=4, K=2) Iteration-2(N=4, K=2)


J=2, while J(2) < N-1(3) J=3, while J(3) <
N-1(3)
Set LA[2]=LA[3], Set
J=3 Set N=3
2 4 8
2 4 8
LA[0] LA[1] LA[2
19
Comparison of Deletion Operation in
an Array
Algorithm: LA[0] LA[1] LA[2] LA[3]
1. Start
2 4 6 8
2. Set J = K
3. Repeat steps 4 and 5 while J < N-1
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop
Location of At the start At the end In the
Deletion of array of array middle of
array
Number of 3 0 [constant 1
Iterations time for
step 6]
Big O (N-1) =O(N) O(1)
Notation Worst Best
20
Summary
⚫ Insertion and deletion are
costly operation if the
selected index is the first or
near to first index of array.

21

You might also like