Sorting In Cobol Program

Sorting In Cobol Program Average ratng: 4,8/5 887 reviews

Hi, Excellent explanation. Although code is giving correct results but it is not using bubble sort technique. In Bubble Sort, 1st number is getting compared with second and then second with third and so on. But as per the logic of code written, it is comparing 1st number with all the remaining numbers and putting the smallest number at first place. Now it will pick second number and compare with rest of the numbers and so on. I am not sure which type of sort is this but definitely results are correct.

SORTING FILES IN COBOL. We shall not cover the MERGE facility of the COBOL SORT statement in this. A complete RM-COBOL program which sorts the PERSONEL.

COBOL Sorting COBOL Sort Statement The Sort Statement Specifies:. The source of the records to be sorted (source file). The Key Field(s). The sequencing desired (ascending or descending). The destination for the sorted files (destination file). The name of a sort work-file that will be used by the sort-merge routine to accomplish the sort. What the Sort Statement Does.

Opens the source file, destination file, and sort work-file. Reads the records in the source file and copies them to the sort work-file. Sorts the records in the sort work-file according to the specified key fields. Copies the sorted records from the sort work-file into the destination file.

SortingCobol

Closes the source file, destination file and sort work-file. Preparing for the SORT In the Environment Division. SELECT source-file ASSIGN TO DISK “A:source.dat” ORGANIZATION IS LINE SEQUENTIAL. SELECT destination-file ASSIGN TO DISK “A:destin.dat” ORGANIZATION IS LINE SEQUENTIAL. SELECT sort-work-file ASSIGN TO DISK “A:destin.dat”. In the DATA DIVISION SD sort-work-file DATA RECORD IS sort-buffer.

01 sort-buffer. 05 FILLER PIC X( ). 05 key-field-1 PIC. 05 FILLER PIC X( ). 05 key-field-2 PIC.

In defining the sort-buffer, you only need to define the key fields you want to sort on. All the other fields may be defined as FILLER. Be sure to maintain the same offsets from the beginning of the record as in your source-file record. The SORT Statement SORT sort-work-file ON ASCENDING (or DESCENDING) KEY key-fld-1 ON ASCENDING (or DESCENDING) KEY key-fld-2 USING source-file GIVING destination-file. Sort Example: 1. In the environment division SELECT stock-file ASSIGN TO DISK “A:stock.dat” ORGANIZATION IS LINE SEQUENTIAL.

SELECT sorted-file ASSIGN TO DISK “A:sorted.dat” ORGANIZATION IS LINE SEQUENTIAL. SELECT work-file ASSIGN TO DISK “A:sort.wrk”. In the DATA Division SD WORK-FILE DATA RECORD IS SORT-REC. 01 SORT-REC 05 INVOICE-SORT PIC X(6).

File Sorting In Cobol

05 FILLER PIC X(2). 05 SALESPERSON-SRT PIC X. 05 FILLER PIC X(71). In the Procedure Division SORT WORK-FILE ON ASCENDING KEY SALEPERSON-SRT ON ASCENDING KEY INVOICE-SORT USING STOCK-FILE GIVING SORTED-FILE.

Sorting In Cobol

Source file = STOCK-FILE. Destination file = SORTED-FILE. KEY Fields = SALESPERSON-SRT (primary); INVOICE-SORT (secondary). Sequencing = ASCENDING.

Sort-work-file = WORK-FILE SORT with INPUT/OUTPUT Procedures SORT sort-work-file ON ASCENDING (or DESCENDING) KEY key-fld-1 ON ASCENDING (or DESCENDING) KEY key-fld-2 INPUT PROCEDURE IS section-name-1 OUTPUT PROCEDURE IS section-name-2. Example SORT WORK-FILE ON ASCENDING KEY SALEPERSON-SRT ON ASCENDING KEY INVOICE-SORT INPUT PROCEDURE IS 1000-PRE-SORT OUTPUT PROCEDURE IS 2000-POST-SORT. Addition Statements Needed with SORT with INPUT/OUTPUT Procedures GO TO Paragraph-name. RELEASE sort-buffer FROM dataname. (similar to write) RETURN sort-work-file RECORD INTO dataname AT END statement. (similar to read) SORT RULES.

COBOL - Subroutines

Never open or close the sort work-file. Define the sort work-file with an SD entry (not an FD). Do not open the source file or destination file prior to the SORT (when using SORT with USING/GIVING option). They must be closed when the SORT statement executes. The sort work-file must be assigned to disk, a direct access storage device.

Use RETURN and RELEASE when referencing the sort work-file (when using the INPUT/OUTPUT procedures method). Neither an INPUT or OUTPUT procedure may contain a SORT statement. Neither an INPUT procedure nor an OUTPUT procedure may reference a paragraph or section outside the procedure. To exist a section, branch to its last paragraph using a GO TO statement. That last paragraph may only contain an EXIT statement. The use of GO TO is restricted to use in INPUT/OUTPUT procedures for COBOL!!!

Don’t try to use it elsewhere.