Documente Academic
Documente Profesional
Documente Cultură
DATA STRUCTURES
MODULE 1
DATA
The term data means value or set of values. It can be used in both singular and plural
terms. The following are some examples of data:
34
12/01/1965
ISBN 81-203-0000-0
│││
Pascal
In the above mentioned example values are represented in different ways. Here each
value or a collection of all such value is termed data.
ENTITY
An entity is one that has certain attributes and which may be assigned values. For
example an employee in an organization is an entity, and the possible attributes are:
Entity : EMPLOYEE
INFORMATION
The term information is used for data with its attribute(s).In other words, information
can be defined as meaningful data or processed data.
For example the set of data represented above becomes information related to a person when
the user imposes meanings as mentioned below in Table 1.1.
Data Meaning
Information
Procedur _2
e to
Data Information
process
data _3
Fig: Information
1.1 _i
DATA TYPE
A data type is a term which refers to the kind of data that may appear in a
computation of data.
DataTable 1.2 illustrates the data and its corresponding
Data Type data type.
38 Numeric
(Integers)
12/01/1965 Date
ISBN 81-203-0000-0 Alphanumeric
│││ Graphics
21, 25, 28,30,35,38 Array of Integers.
Pascal Nick name
Table
1.2
Retrieval of stored data: Data retrieved from the computer should be in such a
way that the user should understand it.
Basically a data structure constitute the fundamentals of computer science and implies
the Domain (The range of value that the data may have), Function (This is the set of
operations that can be legally be applied in the data object), Axioms (This refers to the set of
rules with which the different operations belonging to the function can actually be
implemented). Data Structures is the way of organizing data to do operations on a set of data.
The types of data include numeric data and Alpha numeric data. The data may be a single
value or a set of values, which is to be processed and organized .This organization of a
collection of data leads to structuring of data. Therefore data structure deals with structuring
of data.
A data structure D is a triplet, that is, D= (D, F, A) where ‘D’ is the set of data
objects, ‘F’ is a set of functions, and ‘A’ is a set of rules to implement the functions.
F = (+,-,*, /, %)
DEFINITION:
The data structure with specific operations such as insertion, deletion, searching,
sorting, merging, traversing and display are called Abstract Data Type (ADT), which are user
defined data types.
Item →Components
Θ Accessing methods.
Θ Degree of associativety.
The data structure affects both structural and functional aspects of a program.
The Algorithm is a step by step procedure to solve a problem and the data structure is the
way of organizing data, maintaining the logical relationship.
1. Primitive
DATA
SRUCTURE
NON
PRIMITIVE
PRIMITIVE
DATA
DATA
STRUCTURES
SRUCTURE
TRE SE
ARRA STAC LIS QUEU GRAP TABL
E T
Y K T E H EsPH
Fig: 1.2
These are the basic data structures which are directly operated by machine
instructions.
These are most sophisticated data structures which are derived from Primitive data
structures .The non-primitive data structures emphasize on structuring of a group of
homogeneous or heterogeneous data items.
The common operations done on data structure are categorized into four types:
1. CREATE – The create operations result in retrieving memory for the program
elements which takes place either during compile time or run time.
2. DESTROY or DELETE – This option destroys the memory space allocated for a
specified Data structures.
3. SELECTION – This Option deals with accessing a particular data within the data
structure.
a. Searching – The searching option finds the presence of a derived data item in a list of
data items.
b. Sorting – is the process of arranging all data items in the data structure in a particular
order, either in the descending order or ascending order.
c. Merging – is a process of combining the data items of two different data list into a
single sorted list.
Properties:
Θ The index of the first element is at location 0, That is, a[0] till a[n].
Θ The number of elements stored in an array is the size of the array represented b y
( Upper bound – Lower bound ) + 1
For reading,
1. For (i=0;i<n;i++)
2. {
3. scanf (“%d”,&a[i]);
4. }
For Writing,
6. {
7. printf(“%d”,a[i]);
8. }
LIST A list or linear linked list is defined as variable number of data items .In
dynamic representation of a list, every data item is represented in structures called ‘nodes’
where each node contains two fields. One field for storing ‘data ‘known as Information field
and other for storing address of the node called as Pointer field.
A B C D
Fig:
1.3
Based on the type of linkages between the nodes there exists various types of node as,
POP
PUSH
TOP
Fig: 1.4
QUEUE A queue is a linear data structure where operations are done at both
ends of the structure. It is of the type FIFO (First In First Out).
FRON REAR
T 45 57 80 90 23
77
Fig:
1.5
In the queue all insertions are done from the front end of the queue called FRONT,
and all deletions are done at other end referred as REAR end.
• The special data item represented at the top of the tree is called root.
• The remaining data items are partitioned into number of mutually exclusive or
disjoint subsets and each subset represents a tree structure called sub tree.
The left subset denotes left-sub tree and the right subset represents a right
sub-tree.
• The tree grows in length towards the bottom in the data structure.
D E F G
Fig: 1.6
GRAPH
A B
G
D C
Fig:
1.7
ALGORITHM
1. Input
2. Output
An algorithm can be distinguished from a program. A program may not be finite that
is it may be non terminating. For e.g. operating system is a program that is waiting infinitely
in a loop.
ALGORITHM SPECIFICATION
Pseudocode conventions
We describe an algorithm using a natural language like English, and the resulting
instructions must be definite. Another possibility is Graphic representations called
flowcharts, but it works only if the algorithm is small and simple. Pseudocode that resembles
C also can be used.
Algorithm is defined as finite step by step list of well defined instructions for solving
a problem. The algorithmic notation refers to the format of describing an Algorithm. This
format of formal presentation of an Algorithm consists of two parts:
1) The first section tells the purpose of the Algorithm, identifies the variables which
occur in the Algorithm and list of Input data.
2) The second part of the Algorithm consists of list of steps that needs to be executed.
# Blocks are indicated with matching braces: {and}.That is, a compound statement or a .
The steps of the Algorithm are executed one after the other beginning with step1
and executed downwards, ore control moves to the respective location depending on the
step number, mentioned .i.e., The control can be transferred to any step n of the algorithm
by the statement “Go to Step n” or such statements can be avoided by certain control
structures.
An algorithm can obtain self contained modules and Three types of logic or flow of
control called,
Sequence Logic
In selection logic all the modules are executed in sequence .the sequence may be
referred explicitly by numbered steps or implicitly by the order in which modules are
written.
Algorithm
Module
A
Module B
Module C
Fig:
1.8
Selection Logic
called conditional structures or “If Structures”. The Statement ends with a structure
statement as below.
[End of If structure]
If condition, then:
[Module A]
[End of If structure]
If condition, then:
[Module A]
Else :
[Module B]
[End of If structure]
3. Multiple Alternatives :
[Module A1]
[Module A2]
[Module Am]
Else :
[Module B]
[End of If structure]
Iterative Logic
The two types of structures involving loop are “For Loop” and “While Loop”. Each
type begins with a “Repeat” statement and followed by a module, called the body of the
loop. The end of structure is by the statement, [End of Loop]
1) Repeat For k = R to S by T:
[Module]
[End of Loop]
The Repeat – For loop uses an index variable as K, to Control the loop. The ‘R’ is
called the initial values the end value or the test value, and T the increment.
[Module]
[End of Loop]
They are executed from left to right. The Algorithm is completed with the
statement “Exit”.
3) Comments: Each step can contain comment in brackets which indicates the main
purpose of the step. It usually appears at the beginning or at the end of the step.
# Variable names
Variable names will use capital letters as MAX, DATA etc. Single letter names of
variables are used as counters or subscripts in capital letters (K and N) or lowercase
letters (k and n).
# Assignment statement
<Variable>:= <expression>;
# Boolean values
There exist two Boolean values TRUE and FALSE. To produce this the logical
operators and, or, and not and relational operators < ,<=,=,!= ,>= and > are used. Elements
of multidimensional array are accessed using [ and ] .The (i,j)th element of the array is
denoted as A[i,j],Array indices starts at zero.
Data can be input and assigned to variables by means of a Read statement and output
using Write statement.
# Procedures
# Every Algorithm is identified with a name and consists of heading and a body. The
heading is of the form
Where ‘Name’ is the name of the procedure and ‘parameter list’ is a listing of the
procedure parameters. The body consists of one or more simple or compound statements
within { and }.An algorithm may or may not return values. Simple variables are passed
by value and array or records are passed by reference.
Example1 (Algorithm 1.0): The following algorithm finds and returns the maximum of
n given numbers:
1. Algorithm Max(A,n)
2. // A is an array of size n.
3. {
4. Result := A[1];
5. for i:=2 to n do
7. return Result;
8. }
Recursive Algorithm
One of the efficient methods to make a program readable is to make it modular. That
is, divide the program in to a number of functions. The view of the function is that it is
invoked from one point and it is executed and returns the result to the invoking point.
Recursion is the method by which a function calls itself repeatedly. An Algorithm is said to
be recursive if it calls itself, or if the same algorithm is called in the body
2. {
4. Else
6. }
Run time
Space Requirement
COMPLEXITY OF ALGORITHMS
d) Are procedure created in such a way that they perform logical subfunctions?
Space complexity
A space complexity analysis analyzes, how much memory space does each program
occupies to run to completion. Actually we are not calculating the actual space required
for a program, we are just taking an estimate.
The space needed by each of the programs is the sum of the following components.
1. A fixed part that is independent of the characteristics (number and size of the inputs
and outputs, space for the program code, space for variables, constants etc.
2. A variable part that consists of the space needed for the variables that is dependent on
the particular problem instance being solved, the space needed by the referenced
variables and the recursion stack space.
Illustration: To illustrate how to find space complexity.
(Algorithm 1.3) Algorithm abc (a, b, c)
{
return a + b + b * c + (a + b- c)/ (a + b) +4.0;
}
The space needed by the algorithm is the sum of the following components:
1. A fixed part that is independent of the characteristics (e.g., number, size) of
inputs and outputs. This basically includes the instruction space, space for simple
variables and fixed-size component variables, space for constants etc.
Time complexity
The time taken by a program, T (P), is the sum of compile time and run time or the
computer time it needs to run to completion. Compile time doesn’t depend on instance
characteristics. Because a compiled program can be run many times without recompilation.
So run-time only is considered to analyze time complexity. The time taken for a program
depends on the time taken to execute each statement of the program and also the complexity
of each of the statement is different. So time for a program depend on the number of
additions, subtractions, multiplications, assignments etc.
ж Number of comparisons
ж The execution time increases as the Input data set increases in size.
Illustration: To illustrate how to find Time complexity. It is done by two methods as,
1. Step count method
2. {
3. int i;
4. int a=0;
5. for(i= 0 to n-1)
6. {
7. a[i]=i;
8. }
9. }
After introducing the variable ‘count’ in to the program as,
(Algorithm 1.5)
It determines how the computing time increases as the magnitude of the input
increases. In such case the step count (that is s/e, step per statement execution) of an
algorithm can be computed as a function of the magnitude if the input. In the Algorithm
illustrated below Sum, the
time complexity is measured as a function of the number ‘n’ of element being added.
The complexity analysis defines three cases of step count based on runtime,
¡ Best case
¡ Worst case
¡ Average case
Best case
If the algorithm finds the element at the first search itself, it is referred as a best case
algorithm.
Algorithm Sum(a,n) 0 - 0
{ 0 - 0
s :=0.0; 1 1 1
s := s + a[i]; 1 n n
return s; 1 1 1
} 0 - 0
Total 2n
+3
Table
1.3
Worst case
If the algorithm finds the element at the end of the search or if the searching of the
element fails, the algorithm is in the worst case or it requires maximum number of steps that
can be executed for the given parameters.
Average case
The analysis of average case behavior is complex than best case and worst case
analysis, and is taken by the probability of Input data. As the volume of Input data increases,
the average case algorithm behaves like worst case algoprithm.In an average case behavior;
the searched element is located in between a position of the first and last element.
ASYMPTOTIC NOTATION ( O , Ω , θ )
The Asymptotic notation introduces some terminology that enables to make meaningful
statements about the time and space complexities of an algorithm. The functions f and g are
nonnegative functions.
The function f (n) =O (g (n)) (read as f of n is big oh of g of n) iff there exists two
positive constants c and n0 such that f (n) <=cg (n) for all n, where n>=n0.
Suppose we have a program having count 3n+2. We can write it as f (n) =3n+2. We
say that 3n+2=O (n), that is of the order of n, because f (n) <=4n or 3n + 2 <=
4n, for all n>=2.
Another e.g.
Suppose f (n) =10n2+4n+2. We say that f (n) =O (n2) sine 10n2+4n+2<=11n2 for n>=2 But
here we can’t say that f (n) =O (n) since 10 n 2+ 4n+2 never less than or equal to cn,That is ,
10n2+4n+2 != O (n) , But we are able to say that f (n) =O (n3) since f (n) can be less than or
equal to cn3, same as 10n2+4n+2<=10n4, for n>=2.
Normally suppose a program has step count equals 5, and then we say that it has an order O
(constant) or O (1).
• O (1) – We write O (1) to mean a computing time that is constant. Where the data
item is searched in the first search itself.
• O (n) – O(n) is called linear, where all the elements of the list are traversed and
searched and the element is located at the nth location.
• O (n2) – It is called quadratic, where all elements of the list are traversed for each
element. E.g: worst case of bubble sort.
• O (log n) –The O(log n) is sufficiently faster, for a large value of n, when the
searching is done by dividing a list of items into 2 half’s and each time a half is
traversed based on middle element.E.g: binary searching, binary tree traversal.
• O (nlogn) – The O (nlogn) is better than O(n2 ),but not good as O(n),when the list
is divided into 2-halfs and a half is traversed each time.E.g: Quick sort.
OMEGA NOTATION (Ω )
The function f (n) = Ω (g(n)) (read as “ f of n is omega of g of n” ) iff there exists two
positive constants c and n0 such that f(n) >= c * g(n) for all n, where n >= n0.
Suppose we have a program having count 3n+2. we can write it as f(n)=3n+2. And we say
that 3n+2=Ω (n), that is of the order of n, because f(n)>=3n or 3n+2>=3n, for all n>=1.
Normally suppose a program has step count equals 5, and then we say that it has an order
Ω (constant) or Ω (1).
THETA NOTATION (θ )
The function, f(n) = θ (g(n)) (read as f of n is big oh of g of n) iff there exists two
positive constants c1, c2 and n0 such that c1.g(n) >= f(n) <= c2.g(n) for all n, where n >=
n0.
Department of Computer Sciences and Applications,SJCET,Palai
Page 20
Module 1 MCA 202 ADMN 2009 - 10
DATA STRUCTURES
Suppose we have a program having count 3n+2. we can write it as f(n)=3n+2. We say that
3n+2=θ (n), that is of the order of n, because c1. n >= 3n + 2 <= c2. n .after a particular
value of n.
But we cannot say that 3n + 2 = θ (1) or θ (n2) or θ (n 3).
Normally suppose a program has step count equals 5, then we say that it has an order θ
(constant) or θ (1).
If f(n) = 3n+5 then θ (n)
But we cannot express its time complexity as θ (n2) or θ (1).
ARRAYS
45 57 80 90 23
A [6] 77
Fig:
1.9
Address (A[i]) =M + (I - L) * W
W – Word length.
(Algorithm 1.6)
End
End ARRAY.
Explanation:
The function CREATE produces a new, empty array. RETRIEVE takes as input an
array and an index, and either returns the appropriate value or an error. STORE is used to
enter new index-value pairs. RETRIEVE (STORE (A,I,x),j) is explained as ‘to retrieve the jth
item where x has already been store at index i in A, which is equilavaent to checking if I and
j are equal and if so ,x,or is searched for the jth value in the remaining array, A.
Insertion
Deletion
Sorting
Searching
An Array is classified as
1) 1-Dimensional Array.
2) 2-dimensional Array.
3) Multi-Dimensional array.
REPRESENTATION OF ARRAYS
1-Dimensional Array
A 1-d array is an array where all elements are referenced using only one subscript or
index value. It is very important to see how arrays are represented in memory. Computer
memory can be considered as one-dimensional array of locations from 1 to m. So we are
going to see how the multi dimensional arrays are represented in memory. Suppose a one
dimensional array declared as int a[20];The index of the array starts from 0 and ends at 19.
The number of elements in the array is 20. So the number of elements in the array declared as
int a[i] is i+1.
Suppose we have the one dimensional array declared as int a[5]. Then the elements in
the array are a[0], a[1], a[2], a[3], a[4]. The address of the first location, a[0] is the base
address ‘a’. Then address of the location a[1] is a+1. the address of a[4] is a+4. So if we have
an element a[i], then the address of that element a[i] will be a+i. (Here we assume that one
element occupies one location or one byte in memory. Actually an integer element occupies
2 locations or 2 bytes in memory and a character element occupies 1 location).
//LA is a Linear array with N elements and K is any Position with condition
K<=N.The Algorithm inserts an element into kth position of LA //
Set j:=N
Set j: = j-1
End While
Exit.
//LA is a Linear array where N number of elements are present and K is the
index variable, such that K<=N.The Algorithm deletes Kth element from
LA //
End for
Exit.
2-dimensional Array.
A 2-d array is a collection of homogeneous elements where elements are ordered into
a number of rows and columns. Consider the case of a 2 dimensional array declared as int
a[4][5]. We can represent this as a matrix consisting of 4 rows and 5 columns. We can say
that each row of this matrix consists of 5 elements.
The two dimensional array is represented in the Figure: 1.10 given below.
0 1 2 3
X X X X X
0
X X X X X
1 X X X X X
X X X X X
2
Fig: 1.10
Fig:
Module 1 MCA 202 ADMN 2009 - 10
DATA STRUCTURES
The 2 dimensional array A[u1] [u2] may be interpreted as u1 rows, row0, row1, …,
row u1-1, each row consisting of u2 elements. Suppose @ is the address of A [0] [0], then
the address of A [i] [0] is @+i*u2, as there are i rows , each of size u2, preceding the first
element in the ith row. Knowing the address of A [i] [0], we can say that address of A [i] [j] is
then simply as @ + i * u2 + j.
1 2 3
A 4 5 6
=
7 8 9 m =3,
Fig: n= 3
1.10
The total number of elements in the above matrix =m*n.Here, in the matrix A each element
is specified using 2 index variables i and j,That is, A [i][j]
A matrix can be represented in the memory using continuous memory locations .The 2
formats for storing a matrix in the memory are,
In the row – major ordering, the elements in a matrix, is sorted in a row by row manner. In a
column major ordering, the elements are stored as column by column order.
Example
The 2-d Matrix can be illustrated using the Example in the Figure 1.11, of sparse Matrix.
Sparse matrix is as type of matrix stored in 2-d array where the value of the majority of
elements is ‘zero’ or ‘null ‘.
E.g.:
0 2 0
A= 4 5 0
0 0
Fig: 0 m =3,
n= 3
1.12
In a sparse matrix, since there are few non-zero entries using. Using such 2-d array in the
memory results in the disadvantages such as,
Here, every non-zero entry is stored in a 2-d array with reference to its roe value and
column value .ie,it is represented in the form as,(i,j,value).If the number non-zero entries
represented in a sparse matrix is equal to ‘t’,then the row index ‘i’ will range from 0 to ‘t’
and the column index variable will range from 1 to 3 .i.e,represented as A(0 : t,1 : 3 ),which
means ‘t’ non-zero items are stored into 3 various coloumns.This representation reduces
memory wastage and reduces runtime. The type of operations done on a 2-d array includes
searching,sorting,traversing,merging etc.Another one operation performed on a matrix is
computing the transpose martrix,where A( i, j,value) is stored as B( j, i, value).
The matrix in the Figure 1.12 represents a 6 x 6 matrix of the form sparse. In order for the
effective representation of this sparse matrix in the memory, it needs to be represented in the
3-tuple format. It requires 9 rows and 3 columns, As there are 8 non-zero elements, The first
row represents the number for the representation of the row order(m),column order(n) ,and
the rest 8 rows represent the nonzero value and its row index and column index. This matrix
is told to be 3-tuple format, because, the number of columns has the row number, column
number and the last column has the non-zero terms. That is, the entire 6 x 6, 36 entries in the
memory, which actually contains only 8 values, are reduced into the form with 9 columns
and 8 entries. The 3-tuple matrix is represented in the Figure: 1.13.
The Transpose of the above matrix A can be computed using the procedure
TRANSPOSE (A, B), where A is the original matrix in 3-tuple form and B is the Transpose
of A as represented in Figure: 1.14.
15 0 0 22
0 11 3 0 0
0
Module 1 MCA 202 ADMN 2009 - 10
DATA STRUCTURES 0 0 0 -6 0
0
Col 1 Col
0 02 Col 30 Col 4 Col
05 Col
06
0
Row
1 91 0 0 0
Row 0 0
2
Row 0 0 28 0 0
3 0
Row
4 Fig: 1.13
Row
5
Row
6
(Algorithm 1.9)
then [ (B(q,1) B(q,2) B(q,3) ) ← ( A(p,2) , A(p,1), A(p,3) ) //insert next term of B
q←q+1 ]
End
End.
End TRANSPOSE.
Department of Computer Sciences and Applications,SJCET,Palai
Page 27
Module 1 MCA 202 ADMN 2009 - 10
DATA STRUCTURES
6 6 8
1 4
22
1 2 3
1 6
A (0 -15
A (1 2` 2
11
A (2
2 3
A (3 3
A (4 3 4
-6
A (5
5 1
A (6 91
A (7
6 3
A (8 28
Fig:
1.14
6 6 8
1 1
15
1 2 3
1 4
B (0 22
B (1 1 6
-15
2` 2
B (3 11
B (4 2 3
3
B (5
3 4
B (6 -6
B (7 5 1
91
A (8
Fig:
6 1.15 3
28
Three-dimensional Array
The 3-D array assumes 3 dimensions to refer to a value stored.ie, the number of rows,
number of columns, and number of pages. The element in aijk assumes ith row, jth column, kth
page as illustrated in figure:1.15. Consider a three dimensional array A [u1] [u2] [u3]. The
array is interpreted as u1 two dimensional arrays of dimension u2 x u3. to locate A[I] [j] [k],
we first obtain @ + I u2 u3 + j u3 + k as the address of A [I] [j] [k].
Generalizing this, the addressing formula for any element A [i1] [i2] ….[i n] in an n
dimensional array declared as A [u1] [u2] ….[un] may be easily obtained. If @ is the address
for A [i] [0] …[0] then @ + i1 u2 u3….un is the address for A [i1] [0] …[0]. The address for
A [i1] [i2] [0] ….[0] is then @ + i1 u2 u3…un + i2 u3 u4 … un.
Repeating this way, address for A [i1] [i2] … [in] is
@ + i1 u2 u3 …un
+ i2 u3 u4 … un
+ i3 u4 u5 … un
…
…
…
+ in-1 un
+in
i
K
Fig:
1.16
J
ORDERED LISTS
b) To read the list from left to right and from right to left.
e) To insert a new value in the ith position,1<=i<=n+1,making the rest the elements
shifted to the next positions.
f) To delete the element in position i,1<=i<=n causing the elements numbered in i+1,
…n shifted to the previous locations as n-1.
The most common way to represent the ordered list is using an array, where the list
element ai is at location i of the array index. The ordered list can be sequentially mapped by
storing the ai and ai+1 into consecutive locations of i and i+1 of the array. This makes the users
capable to retrieve or modify the values in a random order in constant amount of time.
The problem requiring to process the ordered list can be done using the 1-d arrays.
The classical example for the List processing technique is the manipulation of symbolic
polynomials. This refers to the 4 basic operations as Addition, subtraction, multiplication and
division that could be done on 2 polynomials. Polynomials are told to be symbolic as it
contains the list of coefficients and exponents in any given polynomial.
A polynomial is defined to be a sum of terms where each term is of the form axe ,here
‘x’ is a variable,’ a’ is the coefficient and ‘e’ is the exponent.
Anxn+an-1xn-1+…+a1x +a0 where an ≠0,the degree of A is n.To represent A(x) an ordered list
of coefficients using a one dimensional array of length n+2 is used.
Example: Consider two polynomials A(x) = 3x2 +2x + 4 and B(x) =x4 +10x3 +3x2 +1
3 2 3 1
A [X] 2 0 4
4 4 1 3 10 2
B[X] 3 0 Fig:1
1.17
In the array representation of the polynomial it uses 2m+1 locations, where m is the number
of terms in the polynomial and the first entry in the array represents the total number of
terms.
7 4 1 3 10 2 6 1 2 0
5 Fig:
1.18
The addition of the 2 polynomials can be represented using the procedure PADD.
(Algorithm 1.10)
m ← A(1);
n ← B(1);
p ← q ← r ← 2;
Case // Compare
exponents //
if C (r+1) ≠ 0
: A (p) < B (q) : C (r+1) ← B (q+1); C(r) ←B(q) // Store new term //
q ← q + 2; r ← r + 2 //advance to next
terms //
p ← p + 2; r ← r + 2 //advance to next
terms //
End
End
C( r) ←A (p);
C(r+1) ←A (p+1)
p ←p+2; r← r+2;
End
C( r) ←B (q);
C(r+1) ←B (q+1)
q ←q+2; r← r+2;
End
End PADD
Explanation
The procedure has the Capital letter parameters which are array names. The 3
pointers (p, q, r) are used to designate a term in A, B or C.The basic iteration is governed by
a While loop and blocks of statements are grouped together using square brackets. Variables
‘m’ and ‘n’ refers to the nonzero terms in A and B respectively.