OPERATIONS ON ARRAY DATA
STRUCTURE
Contents
Representation of Arrays in memory
Traversing Linear Array
Insertion into a Linear Array
Deletion into a Linear Array
Sorting (Bubble Sort)
Dr. Suresh Kumar Poonia, Associate Professor( CSE) 17/07/2023
REPRESENTATION OF ARRAYS IN
MEMORY
Single Dimensional Array
• Let LA be a linear array in memory.
• Let Base(LA) be the address of the first/base element.
• So, the address of any element of LA can be computed as-
LOC(LA[k])= Base(LA) + w (K-lower bound)
where w is the number of words per memory cell for the array
LA.
Two Dimensional Array
• Let A be a two-dimensional m * n array.
• Let Base (A) – the address of the first element A[1,1] of A.
• The address LOC(A[J,K]) of A[M,N] can be computed using the
following formula:
(Column-major order): LOC(A[J,K])= Base (A) + w[M(K-1) + (J-1)]
(Row-major order): LOC(A[J,K])= Base (A) + w[N (J-1) + (K-1)]
•
Dr. Suresh Kumar Poonia, Associate Professor( CSE) 17/07/2023
TRAVERSING LINEAR ARRAY
Algorithm 2.1 : (Traversing a Linear Array) This
algorithm traverses a linear array LA with lower
bound LB and upper bound UB.
• 1. [Initialization] Set K=LB
• 2. Repeat Steps 3 and 4 while K<=UB
• 3. [Visit element] Apply PROCESS to LA[K].
• 4. [Increase counter] Set K=K+1
• 5. Exit
Dr. Suresh Kumar Poonia, Associate Professor( CSE) 17/07/2023
INSERTION INTO A LINEAR
ARRAY
Algorithm 2.2: ( Inserting into a Linear array)
INSERT(LA, N, J, ITEM)
Here LA is a linear array with n elements and J is a positive
integer such that J<=N. This algorithm inserts an element
ITEM into the Jth position in LA.
1. [Initialization] Set K=N.
2. Repeat Steps 3 and 4 while K>=J.
3. [Move Kth element downward.] Set LA[K+1]= LA[K].
4. [Decrease counter.] Set K=K-1.
5. [Insert element.] Set LA[J] = ITEM
6. [Reset N.] Set N= N+1.
7. Exit
Dr. Suresh Kumar Poonia, Associate Professor( CSE) 17/07/2023
DELETION INTO A LINEAR
ARRAY
Algorithm 2.3 : (Deleting from a Linear Array)
DELETE(LA,N,J,ITEM)
Here LA is a linear array with n elements and J
is a positive integer such that J<=N. This algorithm
deletes the Jth elements from LA.
1. Set ITEM= LA[J].
2. Repeat for K= J to N-1
3. [Move K+1st upward.] Set LA[K]= LA[K+1]
4. Reset the number N of elements in LA. Set
N=N-1.
5. Exit.
Dr. Suresh Kumar Poonia, Associate Professor( CSE) 17/07/2023
SORTING (BUBBLE SORT)
Algorithm 2.4: (Bubble Sort) BUBBLE(LA,N)
Here LA is an array with N
elements in LA.
1. Repeat step 2 and 3 for Pass= 1 to N-1.
2. [Initialize Pass pointer.] Set PTR=1.
3. Repeat while PTR <=N-Pass [Execute pass]
a) If LA[PTR]> LA[PTR+1] Then
(interchange elements)
i) T = LA[PTR].
ii) LA[PTR] = LA[PTR+1].
iii) LA [PASS+1] = T.
b) [Go for next pass.] PTR= PTR+1.
4. Exit.
Dr. Suresh Kumar Poonia, Associate Professor( CSE) 17/07/2023
DOUBT CLEARING
Dr. Suresh Kumar Poonia, Associate Professor( CSE) 17/07/2023
Dr. Suresh Kumar Poonia, Associate Professor( CSE) 17/07/2023
Dr. Suresh Kumar Poonia, Associate Professor( CSE) 17/07/2023