LINEAR and BINARY searches can be coded using traditional code or using the SEARCH or SEARCH ALL verb. The one that is coded using traditional code uses an Occurs to define the re-occurrence of the elements in the table and the Subscript to control stepping through the table one element at a time using the looping.
A subscript is essentially a pointer that points to one of the elements in the table. The subscript is a traditional field under the control of the programmer. The subscript is usually initialized with a MOVE statement and incremented with an ADD statement (or a SUBTRACT statement if the subscript was being decreased).
In contrast to the subscript, the SEARCH verb uses an index which is defined by name with the table. The actual setup of the index is done through COBOL. The index is limited in use. It must be used with the table that defined it and it must be controlled using either the PERFORM statement with the varying clause or the SET verb. Essentially a programmer has far more control over the manipulation of a subscript than an index. Because the index is used with the SEARCH verb, the restrictions are defined to maximize its effective use in that context.
SEARCH {table-name} [VARYING {index-name}] [AT END processing-code-1] {WHEN condition {processing-code-2} {NEXT SENTENCE } [END-SEARCH] Example: SEARCH ENTRIES AT END MOVE ERROR-MSG-TBL TO ITEM-NAME-PR WHEN ITEM-NUMBER-TBL (ITEM-INDX) = ITEM-NUMBER-IN MOVE ITEM-NAME-TBL (ITEM-INDX) TO ITEM-NAME-PR
A linear search checks the field we need to match against each element in the table to see if they match. Binary Search is on a sorted array. Compare the item to be searched with the item at the center. If it matches, it stops the search else repeat the process with the left half or the right half depending on where the item lies. Based on this decision, we have eliminated half the table from the search. We next look at the middle item in the half that is left and compare it to the element. This will either find a match or eliminate another quarter of the table. Note that in establishing what the middle is if the number of elements is even, the programmer of a binary search can choose to round or truncate. This process continues until either a match is found or it is clear that the element is not in the table.
SEARCH ALL {table-name} [VARYING {index-name}] [AT END processing-code-1] {WHEN condition {processing-code-2} {NEXT SENTENCE } [END-SEARCH] Example: SEARCH ALL ENTRIES AT END MOVE ERROR-MSG-TBL TO ITEM-NAME-PR WHEN ITEM-NUMBER-TBL (ITEM-INDX) = ITEM-NUMBER-IN MOVE ITEM-NAME-TBL (ITEM-INDX) TO ITEM-NAME-PR
When the SEARCH ALL is used, the programmer does not need to set the table index to a starting value because the SEARCH ALL controls it automatically.
Advantages and Disadvantages
SET verb used to manipulate table indexes. An index item has a special internal representation designed to optimize searching, it cannot be used in any type of normal arithmetic operation (ADD, SUBTRACT, etc.) or even in a MOVE or DISPLAY statement. For index items, all these operations must be handled by the SET verb.
The versions of the SET verb shown below are used to assign a value to an index item, to assign the value of an index item to a variable, and to increment or decrement the value of an index item.
SET {index-name-1} TO {index-name-2} {data-name-1 } TO {data-name-2 } {integer }
There is also a special format for adding to or decreasing the index:
SET {index-name} {UP BY } {data-name} {DOWN BY} {integer }
SEARCH | SEARCH ALL |
It is also called a linear or sequential . | It is also called a binary . |
The entries do not need to be in any order. | The table entries must be in some order. |
Initialization & incrementing of an index is required. | Only initialization is required of an index. Incrementing is done automatically by the system. |
SET statement is a must for initializing the index before its usage. | SET statement is not required. |
Multiple when conditions can be coded. | Only one WHEN condition can be coded. |
Multiple arithmetic operators like =, >, <, ><=, >=, NOT= can be used. | Only = operator is allowed. |
Access is slow. | Access is faster. |
It can be used for both single-dimensional & multidimensional arrays. | It is used for only single-dimensional arrays. |
Data inside an array need not be in sorted order. | Data must be in sorted order. |
A well-maintained product backlog is crucial for successful product development. It serves as a single…
Incremental value to the customer refers to the gradual delivery of small, functional parts of…
A Product Market refers to the group of potential customers who might be interested in…
The Professional Agile Leadership - Evidence-Based Management (PAL-EBM) certification offered by Scrum.org is designed for…
The Professional Agile Leadership (PAL I) certification, offered by Scrum.org, is designed to equip leaders…
Choosing the right Scrum Master Certification depends on your current experience and career goals. If…