Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Arrays

Algorithm for traversing an array


(Traversing a Linear Array) Here LA is a linear array with lower
bound LB and upper bound UB. 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 element] Apply PROCESS to LA[K].
4. [Increment counter] Set K := K + 1.
[End of Step 2 loop]
5. Exit.
Algorithm for inserting an element
into an array
(Inserting into a linear array) INSERT (LA, N, K, ITEM)
Here LA is a linear array with N elements and K is a positive integer such that
K <= N. This algorithm inserts an element ITEM into the Kth position in LA.

1. [Initialize counter.] Set J := N.


2. Repeat Steps 3 and 4 while J >= K.
3. [Move Jth element downward.] Set LA[J+1] := LA[J].
4. [Decrease counter.] Set J := J – 1.
[End of Step 2 loop.]
5. [Insert element.] Set LA[K] := ITEM.
6. [Reset N.] Set N := N + 1.
7. Exit.
Algorithm for deleting an element
from an array
(Deleting from a linear array) DELETE (LA, N, K, ITEM)
Here LA is a linear array with N elements and K is a positive
integer such that K <= N. This algorithm deletes the Kth
element from LA.

1. Set ITEM := LA[K].


2. Repeat for J = K to N – 1
[Move J + 1st element upward.] Set LA[J] := LA[J + 1].
[End of loop.]
3. [Reset the number N of elements in LA.] Set N := N - 1.
4. Exit.
Merging Algorithm
• Suppose A is a sorted list with r elements and B is a
sorted list with s elements. The operation that
combines the element of A and B into a single sorted
list C with n=r + s elements is called merging.

5
Merging Algorithm
 Algorithm: Merging (A, R,B,S,C)
Here A and B be sorted arrays with R and S elements respectively. This algorithm
merges A and B into an array C with N=R+ S elements.

 Step 1: Set NA=1, NB=1 and NC=1


 Step 2: Repeat while NA <= R and NB <= S:
if A[NA] ≤ B[NB], then:
Set C[NC] = A[NA]
Set NA = NA +1
else
Set C[NC] = B[NB]
Set NB = NB +1
[End of if structure]
Set NC= NC +1
[End of Loop]
6
Merging Algorithm
 Step 3: If NA > R, then:
Repeat while NB <= S:
Set C[NC] = B[NB]
Set NB = NB+1
Set NC = NC +1
[End of Loop]
else
Repeat while NA <= R:
Set C[NC] = A[NA]
Set NC = NC + 1
Set NA = NA +1
[End of loop]
[End of if structure]
 Step 4: Return C[NC]

7
Important points
• Arrays can be declared as: AUTO(1932: 1984);
where AUTO[K] = value at K.

• Length = UB – LB + 1

• Address of any element of LA is calculated as:


LOC(LA[K]) = Base(LA) + w(K – LB)

• Ex: If Base address = 200; w = 4 words, then find


address of K=1965 (Ans: 332)
Representation of 2D array in memory
(Row-major)
• Let A is 2D (M x N) array and A[J,K] needs to be found.

• Then, for Row-major (with starting index LB1=LB2=1):

LOC(A[J,K]) = Base(A) + w[N(J - 1) + (K - 1)]

Otherwise,
LOC(A[J,K]) = Base(A) + w[N(J - LB1) + (K - LB2)]
Q. 1) Consider a 2D array X whose index set of
first and second dimensions ranges from -3 to
3 and 2 to 5 respectively (i.e X[-3..3 , 2..5]
a) Find the length of each dimension and size of
this 2D array
b) If this array is stored in row major order then
find the address of X[1,3].Given
Base(X)=5000 and w=2 bytes
Q. 2) Given an array A[10,7] is stored in row
major order. An element A[3,3] is stored at
address 1064 and element A[5,4] is stored at
address 1124. Find the address of A[4,6] and
A[1,2]?
Representation of 2D array in memory
(Column-major)
• Similarly, for Column-major (with starting index LB1=LB2=1):

LOC(A[J,K]) = Base(A) + w[M(K - 1) + (J - 1)]

Otherwise,
LOC(A[J,K]) = Base(A) + w[M(K – LB2) + (J – LB1)]
Q.1) Consider a 2D array X whose index set of
first and second dimensions ranges from -3 to
3 and 2 to 5 respectively (ie X[-3..3 , 2..5]
a) Find the length of each dimension and size of
this 2D array
b) If this array is stored in column major order
then find the address of X[1,3].Given
Base(X)=5000 and w=2 bytes
Q. 2) Given an array A[10,7] is stored in column
major order. An element A[3,3] is stored at
address 1064 and element A[5,4] is stored at
address 1124. Find the address of A[4,6] and
A[1,2]?
Sparse Matrices
Many zero values lead to wastage of memory
and number of operations.
Therefore we use sparse matrices
Regular sparse matrices- Lower
triangular matrix
A₁₁ 0 0 0 0
A₂₁ A₂₂ 0 0 0
A₃₁ A₃₂ A₃₃ 0 0
A₄₁ A₄₂ A₄₃ A₄₄ 0
A₅₁ A₅₂ A₅₃ A₅₄ A₅₅

The matrix will be stored as


B={A₁₁, A₂₁, A₂₂, A₃₁, A₃₂, A₃₃, A₄₁, A₄₂, A₄₃, A₄₄, A₅₁, A₅₂, A₅₃, A₅₄, A₅₅}

Total elements =n(n+1)/2


Upper Triangular Matrix
A₁₁ A₁₂ A₁₃ A₁₄ A₁₅
0 A₂₂ A₂₃ A₂₄ A₂₅
0 0 A₃₃ A₃₄ A₃₅
0 0 0 A₄₄ A₄₅
0 0 0 0 A₅₅

C={A₁₁,A₁₂,A₁₃,A₁₄,A₁₅,A₂₂,A₂₃,A₂₄,A₂₅,A₃₃A,A₃₄,A₃₅,A₄₄A₄₅,A₅₅}

Total elements =n(n+1)/2

You might also like