You are on page 1of 3

Chapter: 2 Array

ALGORITHM: TRAVERSING [A, UB, LB, I, N]


This algorithm is used for traversing the elements of an array ‘A’. ‘UB’ is the
upper bound, ‘LB’ is the lower bound of an array and ‘I’ and ‘N’ are counter
variables.
Step#1 [Initialization of counter variables]
I=LB, N=UB
Step#2 [Starting Loop]
While (I <= N)
Step#3 [Display value of array]
Print (A[I])
Step#4 [Increment counter variable]
I=I+1
Step#5 [Finish]
Exit

ALGORITHM: LARGEST_VALUE [A, UB, LB, I, N, Largest]


This algorithm is used to find out the largest element in an array ‘A’. ‘UB’ is the
upper bound, ‘LB’ is the lower bound of an array, ‘Largest’ is a variable that will
hold the largest value of an array and ‘I’ and ‘N’ are counter variables.
Step#1 [Initialization of counter variable]
I=1
Step#2 [Assignment]
Largest= A [1]
Step#3 [Starting Loop]
While (I <= N)
Step#4 IF (A[I] > Largest) Then
Largest= A[I]
Step#5 [Increment counter variable]
I=I+1
Step#6 [Display Result]
Write (“Largest element in array is =”, Largest)
Step#7 [Finish]
Exit

1|Page
Chapter: 2 Array

ALGORITHM: INSERTION_AT_LAST [A, UB, I, N, DATA]


This algorithm is used for inserting an element in an array ‘A’ at last location. ‘UB’
is the upper bound of array, ‘Data’ is the new value to be inserted and ‘I’ and ‘N’
are counter variables.
Step#1 [Initialization of counter variable]
UB=N
Step#2 [Input value]
Read (Data)
Step#3 [Increment counter variable N]
N=N+1
Step#4 [Insert value at last location]
A[N+1] ← Data
Step#5 [Finish]
Exit

ALGORITHM: INSERTION [A, UB, I, N, K, Item]


This algorithm is used for inserting an element in an array ‘A’ at any location. ‘UB’
is the upper bound of array, ‘Item’ is the new value to be inserted, ‘K’ is the
location number where the new value will be inserted and ‘I’ and ‘N’ are counter
variables.
Step#1 [Initialization of counter variables]
I=N
Step#2 [Starting Loop]
While (I >= K)
Step#3 [Moving element forward]
A[I+1] ← A[I]
Step#4 [Decrement counter variable ‘I’]
I=I-1
Step#5 [Add new element]
A[K] ← Item
Step#6 [Increment counter variable ‘N’]
N=N+1
Step#7 [Finish]
Exit

2|Page
Chapter: 2 Array

ALGORITHM: DELETION_AT_LAST [A, UB, I, N, TEMP]


This algorithm is used to delete an element from an array ‘A’ from last location.
‘UB’ is the upper bound of array, ‘TEMP’ is a variable that will store the deleted
value and ‘I’ and ‘N’ are counter variables.
Step#1 [Check underflow]
IF UB=NULL
Print (“Array is empty”)
Step#2 [Delete last element]
TEMP=A[N]
Step#3 [Decrement counter variable N]
N=N-1
Step#4 [Finish]
Exit

ALGORITHM: DELETION [A, UB, I, N, TEMP]


This algorithm is used to delete an element from an array ‘A’ from any location.
‘UB’ is the upper bound of array, ‘TEMP’ is a variable that will store the deleted
value, ‘K’ is the index of the element to be deleted and ‘I’ and ‘N’ are counter
variables.
Step#1 [Initialization of counter variable]
I=K
Step#2 [Delete element]
TEMP=A[K]
Step#3 [Starting Loop]
While (I < N)
Step#4 [Moving element backward]
A[I] ← A [I + 1]
Step#5 [Increment counter variable ‘I’]
I=I+1
Step#6 [Decrement counter variable ‘N’]
N=N-1
Step#7 [Finish]
Exit

3|Page

You might also like