DS5-6
DS5-6
Arrays
Arrays: - Linear DS Homogeneous (same
type) data elements.
DATA
1 126
2 127 1 2 3 4 5 6
3 123 126 127 123 89 98 833
4 89
5 98
6 833
Arrays
Easy to traverse
Easy to search & sort
Used to store relatively permanent collections of
data.
Types of arrays:-
Linear arrays
Two dimensional arrays
Multidimensional arrays
Representation of Linear array
LOC(LA[K]) = address of the element LA[K] of
the array LA
LOC(LA[K])=Base(LA)+w(K-lower bound)
Where w is the number of words per memory cell for the
array LA.
Traversing Linear Arrays
Traversing: - Accessing each records exactly once.
Algorithm:-
(Traversing a LA) Here LA is a linear array with
lower bound LB & upper bound UB. This algo
traverses LA applying an operation PROCESS to
each element of LA.
1. [Initialize counter] Set K:=LB.
2. Repeat Steps 3 & 4 while K≤UB.
3. [Visit element] Apply PROCESS to LA[K].
4. [Increase counter] Set K:=K+1.
5. [End of Step 2 loop.]
6. Exit.
Insertion of LA
Algorithm:-
INSERT(LA,N,K,ITEM), here LA is a linear array
with N element & K is a +ve integer. This algo
inserts an element ITEM into the Kth position in LA.
1. [Initialize counter.] Set J := N.
2. Repeat Steps 3 & 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.
Deletion of LA
Algorithm:-
DELETE(LA,N,K,ITEM), here LA is a linear array
with N element & K is a +ve integer. This algo
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 no. N of elements in LA] Set N:=N-1.
4. Exit.
Thank You!!!