Download Data File Structures

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Lattice model (finance) wikipedia , lookup

B-tree wikipedia , lookup

Red–black tree wikipedia , lookup

Quadtree wikipedia , lookup

Interval tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
Subject
: Data and File Structure (DFS)
1. What is sparse matrix? Describe the representation of sparse matrix?
A matrix with many zero entries is called sparse matrix.
Example:
7
0
A= -2
0
0
0
0
0
4
In the above matrix, 6 elements are zero out of a 9 elements. So it is a sparse
matrix. Each element of a matrix is uniquely characterized by its row and column
position, say i, j.we store a matrix as a list of 3-tuple of the form
(i,j,value)
The above sparse matrix store as triples in the array A (0: t, 1:3), when t=3 is in the
number of non zero terms.
1
2
3
A=
0
3
3
3
1
1
1
7
2
2
1
-2
3
3
3
4
The elements A(0,1) and A(0,2) contain the number of rows and columns of the
matrix. A (0, 3) contains the number of non zero terms.
2. Discuss the structure and various operations performed over stack.
A stack can be defined as an ordered list in which all insertions and deletions are
made at one end called the top.
Associated with the object stack there are several operations that are necessary:
CREATE(S) which creates S as an empty stack;
ADD(I,S) which inserts the element I onto the stack S and returns the new stack;
DELETE(S) which removes the top element of stack S and returns the new stack;
TOP(S) which returns the top element of stack S;
ISEMTS(S) which returns true if S is empty else false;
structure STACK (item)
1 declare CREATE( ) -> stack
2
ADD (item, stack) -> stack
3
DELETE (stack) -> stack
4
TOP (stack) -> item
5
ISEMTS(stack) -> Boolean;
6
for all S £ stack, i £ item let
7
ISEMTS (CREATE) :: = true
8
ISEMTS (ADD(i, S)) :: = false
9
DELETE (CREATE) :: = error
10
DELETE (ADD(i, S)) :: = S
11
TOP (CREATE)
:: = error
12
TOP (ADD(i, S))
:: = i
13 end
end STACK
The ADD and DELETE operations are only a bit more complex.
procedure ADD (item, STACK, n, top)
// insert item into the STACK of maximum size n; top is the number of elements
currently in STACK //
if top >= n then call STACK_FULL
top<- top +1
STACK (top) <- item
end ADD
procedure DELETE (item, STACK, n, top)
// removes the element of STACK and stores it in item unless STACK is empty //
if top <= 0 then call STACK_EMPTY
item->STACK (top)
top<- top -1
end DELETE
3. Write an algorithm for infix expressions into postfix expressions?
Let Q be an arithmetic expression written in infix expression. The operator
symbol is placed between two operands is called infix notation. Let P be an arithmetic
expression written in postfix expression. The operator symbol is placed after the
operands is called postfix notation.
The following algorithm transforms the infix expression Q into its equivalent of
postfix expression P. The algorithm uses a stack to temporarily hold operators and left
parentheses. The postfix expression P will be constructed from left to right using the
operands from Q and the operators which are removed from STACK. We begin by
pushing a left parenthesis onto STACK and adding a right parenthesis at the end of Q.
The algorithm is completed when the STACK is empty.
ALGORITHM
POLISH (Q, P)
Suppose Q is an arithmetic expression written in infix notation. This algorithm
finds the equivalent postfix expression P.
Push “(“onto STACK, and add “)” to the end of Q.
Scan Q from left to right and repeat Steps 3 to 6 for each element of Q
until the STACK is empty:
If an operand is encountered, add it to P.
If a left parenthesis is encountered, push it onto STACK.
If an operator  is encountered, then:
a) Repeatedly pop from STACK and add to P each operator (on the top of
STACK) which has the same precedence as or higher precedence than .
b) Add  to STACK.
[End of If structure.]
1.
2.
3.
4.
5.
6.
If a 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.]
7. Exit.
Example:
Consider the following arithmetic infix expression Q
Q: A + (B * C – (D/ E  F) * G) * H)
After the conversion from infix expression into postfix expression
P: A B C * D E F  / G * - H * +
This is the required postfix equivalent of Q.
Figure:
The status of STACK and of the string P as each element of Q is scanned.
Symbol Scanned
STACK
Expression P
a.
A
(
A
b.
+
(+
A
c.
(
(+(
A
d.
B
(+(
AB
e.
*
(+(*
AB
f.
C
(+(*
ABC
g.
-
(+(-
ABC*
h.
(
(+(-(
ABC*
i.
D
(+(-(
ABC *D
j.
/
(+(-(/
ABC* D
k.
E
(+(-(/
ABC *DE
l.

(+(-(/
ABC *DE
m.
F
(+(-(/
ABC *DEF
n.
)
(+(-
ABC *DEF/
o.
*
(+(-*
ABC *DEF/
p.
G
(+(-*
ABC *DEF/G
q.
)
(+
ABC *DEF/G*-
r.
*
(+*
ABC *DEF/G*-
s.
H
(+*
ABC *DEF/G*-H
t.
)
ABC *DEF/G*-H*+
4. Write a procedure to add two polynomials using linked list and give one
example.
The polynomial equation can be represented in the form of,
A(X) = am X em + …. + a1 X e1
Where the ai are non zero co-efficient with exponent ei such that
em > em-1 > ………> e 2 > e1 >= 0
Each term will be represented by node. A node will be of fixed size having 3 fields
which represented the coefficient and exponent of a term plus a pointer to the next term.
COEF
EXP
LINK
POLYNOMIAL ADDITION
Procedure PADD (A, B, C)
// polynomial A and B represented as singly linked lists are summed to form the new
list named C //
p  A ; q  B // p,q pointers to next term of A,B //
call GETNODE(C); d C // initial node for C,returned later //
while p≠ 0 and q ≠ 0 do
case
: EXP(p) = EXP(q); //equal exponent//
x COEF(p) + COEF(q)
if x ≠ 0 then call ATTACH(x,EXP(p),d)
p  LINK(p); q  LINK(q); // advanced to next terms //
: EXP(p) < EXP(q);
call ATTACH ( COEF(q) ,EXP(q) ,d)
q  LINK(q); // advanced to next terms //
: else :call ATTACH (COEF(p),EXP(p),d)
p  LINK(p); // advanced to next term of A //
end
end
while p≠ 0 do
call ATTACH( COEF(p), EXP(p) ,d)
p  LINK(p);
end
while q≠ 0 do
call ATTACH( COEF(q), EXP(q), d)
q  LINK (q);
end
LINK (d)  0; t C; CLINK(C)
Call RET(t)
End PADD
Example
For instance the polynomial A=3x^ 14+2x^8+1 would be stored as
3
A
14
2
8
1
0
-3
10
10
0
While B=8x^14-3x^10+10x^6 would like
8
B
14
A
3
14
p
2
8
1
0
0
B
8
14
q
-3 10
10
6
0
C
11 14
A
3
14
2
8
p
1
0
0
B
8
14
-3 10
q
10
6
0
C
11 14
-3 10
0
6
0
EXP(p) = EXP(q)
EXP(p) < EXP(q)
A
3
14
2
8
p
1
0
0
B
8
14
-3 10
10
6
q
0
C
11 14
-3 10
2
8
0
EXP(p) > EXP(q)
and
so an
C
11 14
-3 10
2 8 0
10 6
1 5 0
5. Explain about minimum cost spanning tree.
SPANNING TREE
Let G= (V,E) be an undirected connected graph. A sub graph t = (V,E’) of G is a
spanning tree if and only if ‘t’ is a tree.
A spanning tree with minimum cost is called minimum cost spanning tree.
Fig An undirected graph and three of its spanning trees
FINDING MINIMUM COST SPANNING TREE
1
28
1
10
2
10
16
14
2
16
14
6
7
3
6
7
3
24
25
18
12
25
12
5
5
4
4
22
22
Fig A : Graph and its minimum cost spanning tree
(a)PRIM’S ALGORITHM
 Let G be an undirected connected graph.
 N,E be set of nodes and edges respectively.
 Assume let B a set of nodes and T be a set of edges. Initially B contains a single
arbitrary node and T is empty.
 At each step prim’s algorithm looks for the shortest possible edge (u,v) such that
u B and v  B.
 It then adds ‘v’ to B and (u,v) to T.
 In this way, the edges in T form a minimum spanning tree for the nodes in B.
 We continue the process as long as B  N.
1
1
10
2
10
2
6
7
3
6
7
3
25
5
5
4
(a)
4
(b)
1
1
10
10
2
6
2
7
3
6
25
7
3
25
12
5
5
4
4
22
22
(c)
(d)
1
1
10
10
2
2
16
6
7
16
14
3
6
25
7
3
25
12
12
5
5
4
4
22
22
(e)
(f)
Fig Stages in Prim’s algorithm
ALGORITHM
1.Algorithm Prim(G(N,E))
2.T=0
3.B<-{an Arbitrary member of N}
4.While B N do
5.{
6. find e=(u,v) of minimum length such that uB and vB
7. T<-T{e}
8. B<-B{v}
9. }
10. return T.
Sequence of steps for constructing a spanning tree
Steps
Edges(u,v)
Solution set(B)
Initial
{1}
1
(1,6)
{1,6}
2
(6,5)
{1,5,6}
3
(5,4)
{1,4,5,6}
4
(4,3)
{1,3,4,5,6}
5
(3,2)
{1,2,3,4,5,6}
6.
(2,7)
{1,2,3,4,5,6,7}
Time complexity of Prim’s algorithm is O (n^)
(b) Kruskal’s Algorithm

Let E be the set of edges in G.

Let N be the set of nodes in G.

T is the set of edges in the minimum cost spanning tree.

Select the minimum cost edge from E. Remove the edge from E. It does not
create a cycle in T then add it to T. Otherwise discard it.

This process will continue as long as E  0.
1
1
10
10
2
6
2
7
3
6
7
3
12
5
5
4
4
(a)
(b)
1
1
10
10
2
2
14
6
16
14
7
3
6
7
3
12
5
12
5
4
(c)
4
(d)
1
1
10
10
2
2
16
14
6
7
16
14
3
7
6
3
25
12
12
5
5
4
4
22
22
(e)
(f)
Fig: Stages in Kruskal’s algorithm.
General Algorithm
1. T=0
2. While ((T has less than n-1 edges) and E 0) do
3. {
4. choose an edge (u,v) from E of lowest cost
5. delete (u,v) from E
6. if(u,v) does not create a cycle in T then
7. add (u,v) to T
8. else discard (u,v)
9. }
Tracing the algorithm using the above graph
Steps
Edge
Action
Considered
Connected
Components
Initial
{1},{2},{3},{4},{5},{6},{7}
1
{1,6}
Accept
{1,6},{2},{3},{4},{5},{7}
2
{3,4}
Accept
{1,6},{2},{3,4},{5},{7}
3
{2,7}
Accept
{1,6},{2,7},{3,4},{5}
4
{3,2}
Accept
{1,6},{2,7,3,4},{5}
5
{5,4}
Accept
{1,6},{2,7,3,4,5}
6
{6,5}
Accept
{1,6,2,7,3,4,5}
7
{7,4}
Reject
8
{5,6}
Reject
9
{1,2}
Reject
Time complexity of Kruskal’s algorithm is O( |E| log |E| ) where E is set of edges of G.
6. Discuss about Insertion Sort algorithm with example?
Suppose an array A with n elements is in memory A[0]…..A[n-1]. The insertion
sort algorithm scans A from A(0) to A[n-1], inserting each element A[k] into its proper
position in the previously sorted sub array A[0]……A[n-1]. i.e
Pass 1: A [0] by itself is trivially sorted.
Pass 2: A [1] is inserted either before or after A[0], so that A[0] & A[1] is sorted.
Pass 3: A [2] is inserted into its proper place in A[0],a[1]. i.e. Before A[0], A[0] &
A[1] or after A[1]. So that A[0], A[1],A[2] is sorted.
Pass n: A [n-1] is inserted into its proper place in A[0]………A[n-2] so that
A[0]……A[n-1] is sorted.
This sorting algorithm is frequently used when n is small.
ALGORITHM INSERTIONSORT (A[0…..n-1])
// Sorts a given array by insertion sort
// Input : An array A[0….n-1] of n orderable elements
// Output : Array A[0….n-1] sorted in nondecreasing order.
For i <- 1 to n-1 do
V <- A[i]
J <- i-1
While j>=0 and A[j] > v do
{
A[j+1] <- A[j]
J <- j-1
}
A[j+1] <- v.
The basic operation of the algorithm is the key comparison A[j] > v.
The number of key comparisons in this algorithm depends on the nature of the
input.
The worst case input is an array of strictly decreasing values. The number of key
comparisons for such an input is
z
Cworst (n) = Ө(n )
In the best case, the comparison A[j] > v is executed only once on every iteration
of the outer loop. Thus for sorted arrays, the number of key comparison is
Cbest (n) = Ө(n)
Example for Insertion Sort
28
49
Pass 1
28
49
Pass 2
28
49
Pass 3
20
28
Pass 4
15
20
Pass 5
15
20
20
20
20
49
28
28
Cavg(n) ≈ n2/4 Ө(n
2
)
15
15
15
15
49
49
93
93
93
93
93
33
7. Discuss about graph traversal algorithm.
Many graph algorithms require processing vertices or edges of a graph in a
systematic manner. There are two principal algorithms for doing such traversals.
1. Depth first Search (DFS)
2. Breath first Search (BFS)
Depth first search (DFS)
Depth first search starts visiting vertices of a graph at an arbitrary vertex by
marking it as having been visited. On each iteration the algorithm proceeds to an
unvisited vertex that is adjacent to the one it is currently in. This process continues until
a dead end a vertex with no adjacent unvisited vertices is encountered. At a dead end,
the algorithm backs up one edge to the vertex it came from and tries to continue visiting
unvisited vertices from these.
If unvisited vertices still remain, the DFS must be restarted at any one of them.
It is convenient to use a stack to trace the operation of DFS. We push a vertex
onto the stack when the vertex is reached for the first time and we pop a vertex off the
stack when it becomes a dead end.
DFT(Depth First Traversal) of a graph is carried out by repeatedly calling DFS
each time with a new unvisited starting vertex.
Algorithm DFS(G)
//Implements a Depth first search traversal of a given graph.
//Input: Graph G=<V,E>
//Output:Graph G with its vertices marked with consequtive integers in the order then
have been first encountered by the DFSTraversal mark each vertex in V with o as a
mark of being “unvisited”
count←0
for each vertex v in V do
if v is marked with 0
dfs(v)
dfs(v)
//visits recursively all the unvisited vertices connected to vertex v and assign them in
//numbers in the order they are encountered via global variable count.
count←count++1;
mark v with count
for each vertex w in V adjacent to v do
if w is marked with 0
dfs(w).
Undirected graph
DFS
1
1
2
3
2
3
4
5
6
7
4
5
6
8
7
8
g
h
a
a
g
e
c
c
f
f
h
b
i
e
j
d
d
b
j
i
BFS
DFS
1
1
2
3
1
2
3
4
4
5
6
5
6
2
7
4
3
5
6
7
7
8
8
8
Tree edges are shown with solid lines.
Back edges are shown with dashed lines. Whenever a new unvisited vertex is
reached for the first time, it is attached as a child to the vertex from which is being
reached, such an edge is called a tree edge.
The algorithm may also encounter an edge leading to a previously visited vertex
other than it immediate predecessor. This edge is back edge.
How efficient is depth first search?
The adjacency matrix representation the traversals time efficiency in
Θ(|V|2) and for the adjacency linked list representation, it is in Θ(|V|+|E|) Where |V| is
the number of vertices |E| is the number of edges of the graph.
Applications of DFS
1) Checking connectivity.
2) Checking acyclicity.
3) Finding articulation point.
Breadth first search
The queue is initialized with the traversals starting vertex, which is
marked as visited. On each iteration, the algorithm identifies all unvisited vertices that
are adjacent to the front vertex, makes them as visited, and adds them to the queue,
after that
the front vertex is removed from the queue.
Algorithm BFS(G)
//Implements a breadth first search traversal of a given graph.
//Input:Graph G=<V,E>
//Output:Graph G with its vertices marked with consecutive integers in the order they
have been visited by the Traversal.
Mark each vertex in V with 0 as mark of being “Unvisited”.
count←0
for each vertex v in V do
If V is marked with 0
bfs(v).
bfs(v)
//visits all the unvisited vertices connected to vertex V and assigns them the numbers in
the order they are visited via global variable count.
count←count+1;
mark V with count and initialize a queue with V.
while the queue is not empty do
for each vertex w in V adjacent to the front vertex V do
If w is marked with 0
count←count +1;mark w with count add w to the queue.
Remove vertex V from the front of the queue.
Breadth- first search forest
The traversals starting vertex serves as the root of the first tree is such a forest.
Whenever a new unvisited vertex is reached for the first time, the vertex is
attached as a child to the vertex it is being reached from with an edge called a tree
edge. if an edge leading to a previously visited vertex after than immediate predecessor
is encountered, the edge is noted as a Cross edge.
g
a
1
c
2
d
e
h
j
3
4
f
7
6
5
b
i
BFS has the same efficiency as DFS. It is in Ө(|V| +|E|) for the adjacent linked list
representation.
BFS yields a single ordering of vertices because the queue is a FIFO structure,
and hence the order in which vertices are added to the queue is the same order in
which they are removed from it.
Applications
1) Check Connectivity.
2) acyclicity a graph.
3) Minimum Edge Paths.
a
b
e
c
f
a
b
c
d
d
g
e
f
g
h
h
DFS
1. Data Structure
2. No. of vertex Ordering
3.Edge Type
4.Applications
5.Efficiency for
Adjacency matrix
6.Adjacency Linked List
Stack
2
tree and back
Connectivity
Acyclicity
Articulation
Point
Ө(|V|2)
Ө(|V|+|E|)
BFS
Queue
1
tree and cross
Connectivity
Acyclicity
Minimum
paths
Ө(|V|2)
Ө(|V|+|E|)
8. Write short notes on Binary search Tree and AVL Tree?
Binary Search Tree
It is a binary tree whose nodes contain elements of a set of orderable items, one
element per node, so that all elements in the left sub tree are smaller than the element
in the sub tree’s root and all the elements in the right sub tree are greater than it.
Example
20
12
34
8
15
27
42
38
AVL Trees
AVL trees were invented in 1962 by two Russian Scientists, G.M.Adelson-Velsky
and E.M.Landis.
An AVL tree is a binary search tree in which the balance factor of every node,
which is defined as the difference between the heights of the node’s left and right sub
trees, is either 0, or +1 or -1.(The height of the empty tree is defined as -1).
1
2
10
10
0
1
4
1
20
-1
2
9
0
30
1
0
8
0
0
4
-1
2
1
0
30
8
1
9
0
AVL TREE
0
BINARY SEARCH TREE
BUT NOT AVL
An AVL tree requires the difference between the heights of the left and right sub trees of
every node never exceed.
9. Discuss the various terminologies related to Binary Tree .
A binary tree T is a structure defined on a finite set nodes that either:
 Contains no nodes or
 It is comprised of three disjoint set of nodes: a root node, a binary
tree called its left sub tree (TL) and binary tree called its right sub
tree (TR) .
Fig: Binary trees are best described recursively.
TL
TR
Binary tree as a special case of an ordered tree (An ordered tree is a rooted tree
in which the children of nodes are ordered).
The definition itself divides a binary tree into two smaller structure of the same
type, the left sub tree and the right sub tree, many problems about binary trees can be
solved by applying the DAC technique.
The root is the only node in T with no parent. If two nodes have the same parent,
they are called siblings.
A node with no children is an external node or leaf. A non leaf node is an internal
node.
The number of children of a node x in a rooted tree T is called the degree of x.
The length of the path from the root r to a node x is the depth of x in T. the
largest depth of any node in T is the height of T.
Depth 0
Depth 1
Depth 2
Depth 3
Depth 4
Height=4
A recursive algorithm for computing the height of binary tree.
ALGORITHM Height (T)
// computes recursively the height of a binary tree
// Input: A binary tree T
// output: The height of T
If T=Ø return-1
else return max {Height (TL), Height (TR)}
Height empty tree as -1.The maximum of the heights of root’s left and right
Subtree plus1.
We measure the problem’s instance size by the number of nodes n(T) in a given
binary tree T. The number of comparisons made to compute the maximum of two
numbers and the number of additions A (n (T)) made by the algorithm are same:
Recurrence relation for A (n (T))
A (n (T)) =A (n (TL)) + A (n (TR)) +1 for n (T)>0
A (0) = 0
Full (complete) binary tree, each node is either a leaf or has degree exactly 2.
In analysis of tree algorithms to draw the tree’s extension by replacing the empty
Subtrees by special nodes. The extra nodes ( little square ) are called external, the
original nodes ( little circle ) are called internal.
Binary tree
It’s extension
To ascertain the algorithm’s efficiency we need to know how many external
nodes are external binary tree with n internal nodes can have.
The number of extended nodes x is always one or more than the number of internal
nodes n.
x= n + 1
Let T be a extended binary tree with n internal nodes and x external nodes.
Let nL and xL be the number of internal and external nodes in the left Subtree of
T, and let nR and xR be the number of internal and external nodes in the right Subtree of
T, respectively.
Since n>0, T has a root, which is its internal node and hence n=nL+nR+1.
x=xL+ xR=(nL+1)+(nR+1)=(nL+nR+1)+1=n+1.
Returning to algorithm height, c(n), the number of comparisons to check whether the
tree is empty,
C(n)=n+x=2n+1
While the number of addition is,
A(n)=n
Internal path length of an extended binary tree is defined as the sum of the length
of the paths-taken overall internal nodes, from the root to each external node, similarly,
the external path length E of an extended binary tree is defined as the sum of the
lengths of the paths-taken overall external nodes-from the root to each external node.
E = I + 2n
Algorithm seeks to compute the number of leaves in a binary tree:
ALGORITHM Leaf counters (T)
// computes recursively the number of leaves in a binary tree.
// Input: A binary tree T.
// Output: The number of leaves in T.
If T = Ø return 0
Else return leaf counter (TL ) + leaf counter (TR)
TL
TR
Binary tree as a special case of an ordered tree ( An ordered tree is a rooted tree
in which the children of nodes are ordered).
The definition itself divides a binary tree into two smaller structure of the same
type, the left sub tree and the right sub tree, many problems about binary trees can be
solved by applying the DAC technique.
The root is the only node in T with no parent. If two nodes have the same parent,
they are called siblings.
10. Write an algorithm for linear search and binary search?
Searching: linear search
 Let DATA be a collection of data elements in memory, and suppose a specific
ITEM of information is given. Searching refers to the operation of finding the
location LOC of ITEM in DATA, or printing some message that ITEM does not
appear there. The search is said to be successful if ITEM does appear in DATA
and unsuccessful otherwise.
 The complexity of searching algorithms is measured in terms of the number f(n)
of comparisons required to find ITEM in DATA where DATA contains n elements.
 Linear search is a linear time algorithm but that binary search is a much more
efficient algorithm, proportional in time to log 2n.
Algorithm: (Linear Search)
LINEAR (DATA, N, ITEM, LOC)
Here DATA is a linear array with N elements, and ITEM is a given item of
information. This algorithm finds the location LOC of ITEM in DATA, or sets
LOC: =0 if the search is unsuccessful.
1. [Insert ITEM at the end of DATA] Set DATA [N+1]:=ITEM.
2. [Initialize counter] Set LOC: =1.
3. [Search for ITEM]
Repeat while DATA [LOC] ≠ ITEM.
Set LOC: =LOC+1.
[End of loop.]
4. [Successful?] If LOC=N+1, then: Set LOC: =0.
5. Exit.
Binary search
Let DATA is an array which is sorted in increasing numerical order or ,
equivalently , alphabetically . then there is an extremely efficient algorithm, called binary
search, which can be used to find the location LOC of a given ITEM of information in
DATA.
Algorithm: (Binary Search)
BINARY (DATA, LB, UB, ITEM, LOC)
Here DATA is a sorted array with lower bound LB and upper bound UB, and
ITEM is a given item of information. The variables BEG, END and MID denote,
respectively, the beginning, end and middle location of a segment of elements of DATA.
This algorithm finds the location LOC of ITEM in DATA or sets LOC=NULL.
1. [Initialize segment variables.]
Set BEG: =LB, END:=UB and MID=INT((BEG+END)/2).
2. Repeat Steps 3 and 4 while BEG ≤ END and DATA [MID] ≠ITEM.
3. If ITEM <DATA [MID], then:
Set END: =MID-1
Else:
Set BEG: =MID+1.
[End of If structure]
4. Set MID: =INT ((BEG+END)/2).
[End of Step2 loop.]
5. If DATA [MID] =ITEM, then:
Set LOC: =MID.
Else:
Set LOC: =NULL.
[End of If structure.]
6. Exit.