Handling Tables
4.3 Accessing Table Elements
When you omit the VARYING phrase, the first index you specify in the INDEXED
BY phrase becomes the controlling index. Only this index is incremented during
a serial search. Example 4–18 and Example 4–23 show how to perform a serial
search without using the VARYING phrase.
4.3.8.2 Implementing a Binary Search
You can use the SEARCH statement to perform a nonsequential (binary) table
search.
To perform a binary search, you must specify an index name in the INDEXED BY
phrase and a search key in the KEY IS phrase of the OCCURS clause of the table
being searched.
A binary search depends on the ASCENDING/DESCENDING KEY attributes. If
you specify an ASCENDING KEY, the data in the table must either be stored
in ascending order or sorted in ascending order prior to the search. For a
DESCENDING KEY, data must be stored or sorted in descending order prior
to the search.
You can sort an entire table in preparation for a binary search. Use the SORT
statement (Format 2, a Compaq extension), described in the Compaq COBOL
Reference Manual.
During a binary search, the first (or only) index you specify in the INDEXED
BY phrase of the OCCURS clause of the table being searched is the controlling
index. You do not have to initialize an index in a binary search because index
manipulation is automatic.
In addition to being generally faster than a sequential search, a binary search
allows multiple equality checks.
The following search sequence lists the capabilities of a binary search. At
program execution time, the system:
1.
Examines the range of permissible index values, selects the median value,
and assigns this value to the index.
2.
Checks for equality in WHEN and AND clauses.
3.
Terminates the search if all equality statements are true. If you use the
imperative statement after the final equality clause, that statement executes;
otherwise, program control passes to the next procedural sentence, the search
exits, and the index retains its current value.
4.
Takes the following actions if the equality test of a table element is false:
a.
Executes the imperative statement associated with the AT END statement
(if present) when all table elements have been tested. If there is no AT
END statement, program control passes to the next procedural statement.
b.
Determines which half of the table is to be eliminated from further
consideration. This is based on whether the key being tested was specified
as ASCENDING or DESCENDING and whether the test failed because
of a greater-than or less-than comparison. For example, if the key values
are stored in ascending order, and the median table element being tested
is greater than the value of the argument, then all key elements following
the one being tested must also be greater. Therefore, the upper half of the
table is removed from further consideration and the search continues at
the median point of the lower half.
c.
Begins processing all over again at Step 1.
4–18 Handling Tables
Содержание COBOL AAQ2G1FTK
Страница 22: ......
Страница 30: ......
Страница 94: ......
Страница 110: ......
Страница 146: ......
Страница 180: ......
Страница 194: ...Processing Files and Records 6 1 Defi...
Страница 300: ......
Страница 490: ......
Страница 516: ......
Страница 517: ......
Страница 530: ......
Страница 534: ......
Страница 590: ......
Страница 620: ......