Array
Array
Arrays
Linear Arrays
- A Linear array is a list of a finite no. n of homogenous data
elements such that
- elements of the list are stored respectively in consecutive memory
locations.
- elements can be referenced respectively by an index set consisting
of n consecutive numbers.
1 80
2 69 The length of Student_Marks Upper Bound (UB) = 6
3 90 is
Lower Bound (LB) = 1
4 78 Length = UB – LB + 1
5 65 =6–1+1=6
6 75
Student_Marks
Memory view
Algorithm: This algorithm traverses the linear array LA with lower bound LB and
upper bound UB. Here the operation PROCESS may be any operation (e.g. printing etc.)
1. Repeat for K := LB to UB
for 2. Apply PROCESS to LA[K] // Visit each element in LA
loop // End of for loop
3. Exit
Algorithm : This algorithm prints each year and the no. of automobiles sold in that year
from the array AUTO.
Insertion
- Let A be a linear array. Insertion refers to the operation of adding another element to A.
- Inserting an element at the end of A is simply done provided that there is enough
memory space for the new element.
- Inserting an element at the middle of A requires that all the elements following that
position must be moved backward to new locations to accommodate the new element
Deletion
- Deletion refers to the operation of removing an element from A.
- Deleting an element from the end of A is simple. But deleting from the middle of A
requires that all the elements following that position must be moved forward
1 15 1 15 1 15 1 15
2 20 2 20 2 20 2 20
3 25 3 25 3 25 3 28 25 out
4 30 4 30 4 28 28 in 4 30
5 35 5 35 5 30 5 35
6 6 45 45 in 6 35 6 45
7 7 7 45 7
8 8 8 8
Initial state (a) Insertion at the end (b) Insertion at the middle (c) Deletion from the middle
Algorithm for Deletion: This algorithm deletes the K-th element from a linear array
LA which has N elements and assigns the element to ITEM. Here K must be <= N
BUBBLE (DATA, N)
1. Repeat steps 2 and 3 for K =1 to N -1
2. Set PTR:=1 // Initializes pass pointer PTR
3. Repeat while PTR <= N – K // Execute pass
a) IF DATA[PTR] > DATA[PTR + 1], then:
Interchange DATA[PTR] and DATA[PTR + 1]
[End of IF Structure]
b) set PTR:=PTR + 1
[End of Inner Loop]
[End of Step 1 outer loop]
4. Exit
The elements of A with first subscript J and second subscript K are denoted as
Aj,k or A[J, K]
columns
1 2 3 4
1 A[1, 1] A[1, 2] A[1, 3] A[1, 4]
A
2-dimensional 3*4
rows 2 A[2, 1] A[2, 2] A[2, 3] A[2, 4]
matrix
(2, 1)
Column 1
(1, 2) Row 1
(1, 3) A (1, 2)
2-dimensional 2*3 Column 2
(2, 1) matrix
(2, 2)
(2, 2) Row 2 (1, 3)
Column 3
(2, 3) (2, 3)
Similar is the case with 2-D arrays. If A is an m*n 2-D array then the
address of the element A[J, K] is given by –
Row-major order
Column-major order
104 A[2, 1]
rows 2 A[2, 1] A[2, 2] A[2, 3] A[2, 4]
105 A[2, 2]
Row 2
3 A[3, 1] A[3, 2] A[3, 3] A[3, 4] 106 A[2, 3]
107 A[2, 4]
108 A[3, 1]
109 A[3, 2]
Row 3
110 A[3, 3]
111 A[3, 4]
Row-major order
109 A[1, 4]
MATMUL (A, B, C, M, P, N)
1. Repeat Steps 2 to 4 for I = I to M:
2. Repeat Steps 3 and 4 for J = 1 to N:
3. Set C[I, J] := 0 // Initializes C[I, J]
4. Repeat for K = 1 to P:
C[I, J] := C[I, J] + A[I, K] * B[K, J]
[End of inner for loop]
[End of Step 2 middle loop]
[End of Step 1 outer loop]
5. Exit
24 October 2019 Data Structures 19
Eastern University