[USflag] The American Programmer [USflag]


Home
Books on Mainframe Programming
Mainframe Manuals and Tutorials
System Abend codes, Sqlcodes, VSAM/QSAM codes
Everything about the IBM AS/400 Midrange Computer - iSeries
Everything about CICS
Cobol programs, manuals, books
  Sample Cobol code: The Simple, Single File COBOL Program
  Sample Cobol code: The Simple, Single File Report COBOL Program with Record Count or Final Totals
  Sample Cobol code: The Simple, Single File Report COBOL Program with Record Count or Final Totals
  Sample Cobol code: The Sequence Check COBOL Program
  Sample Cobol code: The Record Selection COBOL Program
  Sample Cobol code: The Edit or Validate COBOL Program
  Sample Cobol code: The The One Level Subtotal (Control Break) COBOL Program
  Sample Cobol code: The Three Level Subtotal (Control Break) COBOL Program
  Sample Cobol code: The Sequential File, Batch Update COBOL Program
  Sample Cobol code: The COBOL Sort
  Sample Cobol code: The CASE Structure: COBOL EVALUATE
  Sample Cobol code: Direct Subscripting in COBOL
  Sample Cobol code: The Sequential, or Serial Search in COBOL
  Sample Cobol code: The Binary Search in COBOL
  Sample Cobol code: Loading a Table from a Sequential File in a COBOL program
  Sample Cobol code: The VSAM File Read Sequentially in a COBOL program
  Sample Cobol code: The VSAM KSDS, Read Randomly in a COBOL program
  Sample Cobol code: The VSAM File, Read Randomly in a COBOL program
  Sample Cobol code: VSAM Initial Load in a COBOL program
  Sample Cobol code: VSAM File Maintenance (Add, Change, Delete) in a COBOL program
  Sample Cobol code: VSAM Read Sequentially, with START, in a COBOL program
  Sample Cobol code: Creating a Variable Format File in a COBOL program
  Sample Cobol code: Reading a Variable Format File in a COBOL program
  Sample Cobol code: Creating a Variable Format File with Occurs Depending On in a COBOL program
  Sample Cobol code: COBOL Reading a Variable Format File with Occurs Depending On, in a COBOL program
  Sample Cobol code: COBOL The Table Load with Occurs Depending On
  Sample Cobol/DB2 code: Singleton Select
  Manuals on the COBOL programming language.
  Books on Cobol
  Abend Codes from Cobol programs
Everything about DB2 and SQL
Everything about IMS
Everything about Java and JavaScript
Everything about JCL and JES
Everything about REXX
Everything about zOS, VSAM, Tivoli, Assembler
Everything about TSO, ISPF, Spufi
Site Map and Site Search

           Home   > COBOL   > COBOL program the Binary Search

The Binary Search in COBOL


14. The Binary Search

BINSRCH1.
Similar to The Sequential Search, #13 above, but a possibly faster method of searching when large amounts of data are involved. COBOL uses the SEARCH ALL verb.

The binary search can be used when the entries in the table are in strict order, ascending or descending. It may be a good choice and run faster than the sequential search when there are many (over 100 or so) entries in the table.

Do you know what happens if the entries in the table are not in strict order? There will be no compiler diagnostics, no middle of the night abends, but the SEARCH ALL won’t work right. It may work for some entries, but not for others, and you may think the program works in every case when it actually doesn’t. So the program goes into production and some good records are rejected. After a few complaints, some programmer will be burning the midnight oil trying to track down the source of the problem. Guess who?

Check out your programs carefully. Check the first entry and the last.

Here is the program BINSRCH1:

Compliments of Gabe Gargiulo, author of several recent books on programming and modern languages, available at Amazon.com.


000200 IDENTIFICATION DIVISION.
000300 PROGRAM-ID. BINSRCH1.
000400* The binary search
000500* reads every input record
000600* after looking up the employee's month of hire on a table,
000700* by a sequential search, it writes it out to an output file
000800 ENVIRONMENT DIVISION.
000900 INPUT-OUTPUT SECTION.
001000 FILE-CONTROL.
001100*    INPUT FILE EMP
001200     SELECT INPUT-FILE ASSIGN EMP.
001600*    REPORTFI: A REPORT FILE, PRINTS OUT INFORMATION ON EMPLOYEES
001700*        WITH MONTH OF HIRE, SEND TO PRINTER
001800     SELECT REPORT-FILE ASSIGN REPORTFI.
002200
002300 DATA DIVISION.
002400 FILE SECTION.
002500
002600 FD  INPUT-FILE
002700     RECORDING MODE IS F
003000     RECORD CONTAINS 80 CHARACTERS.
003100 01  INPUT-RECORD.
004700*            INPUT RECORD DESCRIPTION
005000     05  FILLER                        PIC X(08).
005100     05  FILLER                        PIC X(01).
005200     05  ER-EMPLOYEE-NUMBER            PIC X(05).
005300     05  FILLER                        PIC X(01).
005400     05  ER-EMPLOYEE-NAME              PIC X(25).
005500     05  ER-EMPLOYEE-DEPARTMENT        PIC X(05).
005600     05  FILLER                        PIC X(01).
005700     05  ER-EMPLOYEE-SALARY-CODE       PIC X(02).
005800     05  FILLER                        PIC X(01).
005900*     MONTH OF HIRE CAN BE DEFINED AS CHARACTER (ALPHANUMERIC)
006000     05  ER-MONTH-OF-HIRE              PIC X(02).
006100     05  FILLER                        PIC X(29).
003200
003300 FD  REPORT-FILE
003400     RECORDING MODE IS F
003700     RECORD CONTAINS 133 CHARACTERS.
003800
003900 01  REPORT-RECORD                      PIC X(133).
004100 WORKING-STORAGE SECTION.
004200
004300 01  FILE-AT-END             PIC X VALUE 'N'.
004400*
004500 01  SW-VALID-RECORD         PIC X VALUE 'Y'.
004600
006400 01  COUNTERS-AND-ACCUMULATORS.
006500     05  CTR-RECORDS-READ               PIC  9(5)
006600         PACKED-DECIMAL    VALUE 0.
006700     05  CTR-RECORDS-WRITTEN            PIC  9(5)
006800         PACKED-DECIMAL       VALUE 0.
006900
007000 01  TITLE-HEADING-LINE.
007100     05  FILLER                         PIC X(1) VALUE SPACES.
007200     05  FILLER                         PIC X(35)
007300                   VALUE 'EMPLOYEE RECORDS WITH MONTH OF HIRE'.
007400     05  FILLER                         PIC X(04) VALUE SPACES.
007500     05  FILLER                         PIC X(33)
007600                   VALUE SPACES.
007700     05  REPORT-DATE.
007800           10  REPORT-YY PIC 99.
007900           10  REPORT-MM PIC 99.
008000           10  REPORT-DD PIC 99.
008200 01  DETAIL-PRINT-LINE.
008300     05  FILLER                         PIC X(1) VALUE SPACES.
008400     05  DL-MONTH-OF-HIRE               PIC X(09).
008500     05  DL-RECORD-IMAGE                PIC X(80) VALUE SPACES.
008700 01  MONTH-TABLE-LITERALS.
008800*     This is hard coding a table.
008900*     most explanations are meaningless,
009000*     until you study this carefully to see what is happening
009100*     these are fillers, because you
009200*         won't be referring to them directly, by name
009300*     notice how the code (01) is written right
009400*        beside the name (january)
009500*     all the pictures must be the same
009600*     the literals inside of quotes don't have
009700*       to be the same length, but many will code them that way
009800              05     FILLER     PIC X(11)     VALUE '01JANUARY'.
009900              05     FILLER     PIC X(11)     VALUE '02FEBRUARY'.
010000              05     FILLER     PIC X(11)     VALUE '03MARCH'.
010100              05     FILLER     PIC X(11)     VALUE '04APRIL'.
010200              05     FILLER     PIC X(11)     VALUE '05MAY'.
010300              05     FILLER     PIC X(11)     VALUE '06JUNE'.
010400              05     FILLER     PIC X(11)     VALUE '07JULY'.
010500              05     FILLER     PIC X(11)     VALUE '08AUGUST'.
010600              05     FILLER     PIC X(11)     VALUE '09SEPTEMBER'
010700              05     FILLER     PIC X(11)     VALUE '10OCTOBER'.
010800              05     FILLER     PIC X(11)     VALUE '11NOVEMBER'.
010900              05     FILLER     PIC X(11)     VALUE '12DECEMBER'.
011000*     Redefines means that this 01 level item
011100*     occupies the same spot in memory as the one it redefines
011200*     so actually the two 01 levels are
011300*     the same thing with different names and different picture
011400 01  MONTH-TABLE REDEFINES MONTH-TABLE-LITERALS.
011500*     Next item must occur as many times
011600*     as there are fillers in the preceding 01
011700*     its picture or the pictures under it
011800*     must add up to the same number as the
011900*     picture in the fillers above (11 in this example)
012000     05     EACH-MONTH-INFO     OCCURS 12 TIMES
012100*     You need the indexed by clause
012200*     if you're going to use the search verb
012300*     this defines and creates the index -
012400*     so no pictures for the index, please
012600*     The ascending (or descending) key clause
012700*     is required for a binary search
012800*     of course, the data must actually be in order
012900*     or this won't work right
013000            ASCENDING KEY IS EACH-month-number
013010            INDEXED BY MONTH-INDEX.
013100
013200            10  EACH-MONTH-NUMBER           PIC XX.
013300            10  EACH-MONTH-NAME             PIC X(09).
013400
013500 PROCEDURE DIVISION.
013600     PERFORM INITIALIZATION
013700     PERFORM PROCESS-ALL UNTIL
013800        FILE-AT-END = 'Y'
013900     PERFORM TERMINATION
014000     GOBACK.
014200 INITIALIZATION.
014300     OPEN INPUT INPUT-FILE
014400          OUTPUT REPORT-FILE
014500     WRITE REPORT-RECORD FROM TITLE-HEADING-LINE
014600     PERFORM READ-PAR
014700**   accept gets today's date from the system
014800     ACCEPT REPORT-DATE FROM DATE.
014900
015000 PROCESS-ALL.
015100     PERFORM LOOKUP-MONTH
015200     MOVE INPUT-RECORD TO dl-reCORD-IMAGE
015300     WRITE REPORT-RECORD FROM DETAIL-PRINT-LINE
015400
015500     PERFORM READ-PAR.
015600
015700 TERMINATION.
015800
015900     CLOSE INPUT-FILE
016000           REPORT-FILE.
016100
016200 READ-PAR.
016300     READ INPUT-FILE 
016400     AT END
016500         MOVE 'Y' TO FILE-AT-END
016600     NOT AT END
016700        ADD 1 TO CTR-RECORDS-READ
016800     END-READ.
017000 LOOKUP-MONTH.
017100*   When doing a binary search,
017200*     you don't set the index to 1 before doing the search
017300*   (but if you do, it ignores that and still works)
017400*   you search "all" the thing that occurs,
017500     SEARCH ALL EACH-MONTH-INFO
017600*   At end means not found
017700     AT END
017800*     Move 'N' to found-switch
017900      move 'unknown' to dl-month-of-hire
018000*     the when is a condition, like an if.
018100*     you must "when" the thing that occurs or an item under it
018200*      comparing it to something on input record (month of hire )
018300     WHEN EACH-MONTH-number(MONTH-INDEX) = ER-MONTH-OF-HIRE
018400*     Move 'Y' to found-switch
018500*     at this point you have found it - got a match
018600*     so here is where you do what you need to do on a match
018700        MOVE EACH-MONTH-NAME(month-index) TO DL-MONTH-OF-HIRE
018800     END-SEARCH.

Compliments of Gabe Gargiulo, author of several recent books on programming and modern languages, available at Amazon.com.
Here is the input data file EMP: (the next two lines are a column ruler) 1 2 3 4 5 6 123456789.123456789.123456789.123456789.123456789.123456789.12345678 01000 PEARLE E GATES D0001 01 02000 LED BALOON D0002 04 03000 KIT E LITTER D0005 06 04000 MOE PEDD D0504 01 Here is sample JCL: //STEP1 EXEC PGM=BINSRCH1 //STEPLIB DD DSN=your.executable.program.library.goes.here,DISP=SHR //*OF COURSE, THE NEXT LIBRARY NAME MAY BE DIFFERENT AT YOUR COMPANY //EMP DD DSN=userid.COBBOOK.DATA(EMP),DISP=SHR //REPORTFI DD SYSOUT=* //SYSOUT DD SYSOUT=* //SYSUDUMP DD SYSOUT=*

Top of Page

Your email and other personal information will not be given to anyone
[Books Computer]

Home Books for Computer Professionals Privacy Terms |
Site Map and Site Search Programming Manuals and Tutorials The REXX Files Top of Page |