ALGORITHMS
FOR DIFFERENT OPERATIONS ON ONE-DIMENTIONAL ARRAYS:
1.
Algorithm
for traversing an array:
TRAVERSE(A,UB,LB):
Here A is a linear array with LB as lower bound and UB as upper
bound. This algorithm traverses A applying an operation PROCESS to
each element of A.
Step
1:
Set K:=LB
Step
2:
Repeat Steps 3 and 4 while K<=UB
Step
3:
Apply PROCESS to A[K]
Step
4:
Set K:=K+1
Step
5:
Exit.
2.
Algorithm
for insertion in an array:
INSERT(A,N,K,ITEM):
Here A is a linear array with N elements, K is a positive integer
such that K<=N. This algorithm inserts an element ITEM into the
Kth position in A.
Step
1:Set
J:=N
Step
2:
Repeat Steps 3 and 4 while J>=K
Step
3:Set
A[J+1]:=A[J]
Step
4:
Set J:= J-1
[End
of step 2]
Step
5:
Set A[K]:=ITEM
Step
6:
N:=N+1
Step
7:
Exit
3.
Algorithm
for deletion in an array:
DELETE(A,N,K,ITEM):
Here A is a linear array with N elements and K is a positive integer
such that K<=N. This algorithm deletes the Kth element from A.
Step
1:
ITEM:=A[K]
Step
2:
Repeat for J:=K to N-1
Set
A[J]:= A[J+1]
[
End of loop]
Step
3:
Set N:=N-1
Step
4:
Exit
No comments:
Post a Comment