Upper Bound Lower Bound
Upper Bound Lower Bound
Length = UB – LB + 1
Where w is the number of words per memory cell for the array LA.
Traversing Linear Array
This can be accomplished by traversing array, that is, by
accessing and processing ( frequently called visiting ) each
element of array exactly once.
Step 5: Exit.
Inserting into a Linear Array
INSERT ( LA, N, K, ITEM )
Here LA is a linear array with N elements and K is a positive
integer
such that K <= N. This algorithm inserts an element ITEM
into the Kth
position in LA.
Step 1 : Compare A[1] and A[2] and arranged them in desired order, so
that A[1] < A[2]. Then compare A[2] and A[3] and arrange them
so that A[2] < A[3]. Then compare A[3] and A[4] and arrange
them so that A[3] < A[4]. Continue until we compare A[N-1] with
A[N] and arrange them so that A[N-1] < A[N].
Step 2 : Repeat Step 1 with one less comparison; that is, now we stop
after we compare and possibly rearrange A[N-2] and A[N-1].
Step N-1: Compare A[1] with A[2] and arrange them so that A[1] <
A[2].
Note that the variable BEG and END denote, respectively, the
beginning and end locations of the segment under
consideration. The
algorithm compares ITEM with the middle element DATA[MID]
of the
segment, where MID is obtained by
(3,1)
In two dimensional array computer keeps track of Base(A) –
the
address of the first element A[1,1] of A – and computes the
address
LOC(A[J,K]) of A[J,K] using the formula
( Column-major order )
( Row-major order )
Sparse Matrices
Stacks
A Stack is list of elements in which an element may be inserted and
deleted only at one end, called the top of the stack. This means, in
particular, that elements are removed in stack in the reverse order of
that in which they are inserted into the stack.
Special terminology is used for the two basic operations associated into
Stacks.
Highest : Exponential.
Next Highest : Multiplication and Division.
Lowest : Addition and Subtraction.
Notation :
Infix : A+B
Prefix : +AB
Postfix : AB+
Evaluation of Postfix Expression
Algorithm : This algorithm finds the value of an arithmetic expression
P
written in postfix notation.
Step 1 : Add the right parenthesis “)” at the end of P.
[ this act as a sentinel. ]
Step 2 : Scan P from left to right and repeat step 3 & 4 for
each element
of P until the sentinel “)” is encountered.
Step 3 : If an operand is encountered, put it on STACK.
Step 4 : If an operator is encountered, Then:
a) Remove the two top elements of STACK, where A is the
top
element and B is the next-to-top element.
b) Evaluate B operator A.
c) Place the result of (b) back on STACK.
[ end of If structure ]
[ end of step 2 loop ]
Step 5 : Set VALUE equal to the top element on STACK.
Step 6 : Exit.
Transforming Infix Expressions into Postfix
Expressions
POLISH( Q, P )
Suppose Q is an arithmetic expression written in infix notation. This
Algorithm finds the equivalent postfix expression P.
Step 1 : Push “(“ onto STACK, and add “)” to the end of Q.
Step 2 : Scan Q from left to right and repeat step 3 to 6 for each
element of Q until the STACK is empty.
Step 3 : If an operand is encountered, add it to P.
Step 4 : If a left parenthesis is encountered, push it onto STACK.
Step 5 : If an operator is encountered, Then:
a) Repeatedly pop from STACK and add to P each operator
( on the top of STACK ) which has a same precedence
or the higher precedence than an operator encountered.
b) Add operator encountered to STACK.
[ end of if structure ]
Step 6 : If the right parenthesis is encountered, then:
a) Repeatedly pop from STACK and add to P each
operator
( on the top of STACK ) until a left parenthesis is
encountered.
b) Remove the left parenthesis. [ do not add left
parenthesis to
P]
[ end of if structure ]
[ end of step 2 loop ]
Step 7 : Exit.
Queues
A queue is a linear list of elements in which deletion can take
place
only at one end, called the front, and the insertion can take
place
only at the other end, called the rear.
The time and space it uses are two major measures of the
efficiency of an algorithm.