1 / 34

Sorting

Sorting. Figure 14.1 Sorting Vocabulary. NAME YEAR MAJOR Smith 1 Liberal arts Jones 4 Engineering Adams 3 Business Howe 2 Liberal arts Frank 1 Engineering Zev 4 Business Benjamin 4 Business Grauer 3 Liberal arts Crawford 2 Engineering Deutsch 4 Business Makoske 1 Business.

carrie
Download Presentation

Sorting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sorting

  2. Figure 14.1 Sorting Vocabulary NAME YEAR MAJOR Smith 1 Liberal artsJones 4 EngineeringAdams 3 BusinessHowe 2 Liberal artsFrank 1 EngineeringZev 4 BusinessBenjamin 4 BusinessGrauer 3 Liberal artsCrawford 2 EngineeringDeutsch 4 BusinessMakoske 1 Business (a) Unsorted Data

  3. Figure 14.1 (b) Sorted Data, One Key Primary Key: Name (Ascending) NAME YEAR MAJOR Adams 3 BusinessBenjamin 4 Business Crawford 2 EngineeringDeutsch 4 BusinessFrank 1 Engineering Grauer 3 Liberal arts Howe 2 Liberal artsJones 4 EngineeringMakoske 1 BusinessSmith 1 Liberal artsZev 4 Business

  4. Figure 14.1 Sorted Data, Two Keys Primary key: Year (descending)Secondary key: Name (ascending) NAME YEAR MAJOR Benjamin 4 Business Deutsch 4 BusinessJones 4 EngineeringZev 4 Business Adams 3 BusinessGrauer 3 Liberal arts Crawford 2 EngineeringHowe 2 Liberal artsFrank 1 Engineering Makoske 1 BusinessSmith 1 Liberal arts

  5. Figure 14.1 Sorted Data, Three Keys Primary Key: major (Ascending)Secondary Key: year (Descending)Tertiary Key: name (Ascending) NAME YEAR MAJOR Benjamin 4 Business Deutsch 4 BusinessZev 4 Business Adams 3 BusinessMakoske 1 Business Jones 4 Engineering Crawford 2 Engineering Frank 1 Engineering Grauer 3 Liberal arts Howe 2 Liberal artsSmith 1 Liberal arts

  6. Figure 14.2EBCDIC & ASCII Collating Sequences EBCDIC (space). (period, decimal point)< (less than)( (left parenthesis)+ (plus symbol)$ (currency symbol)* (asterisk)) (right parenthesis); (semicolon)- (hyphen, minus symbol)/ (slash), (comma)> (greater than)‘ (apostrophe)= (equal sign)“ (quotation mark) a through z (lower case) A through Z (upper case) 0 through 9 ASCII (space)“ (quotation mark) $ (currency symbol)‘ (apostrophe) ( (left parenthesis) ) (right parenthesis) * (asterisk)+ (plus symbol) , (comma) - (hyphen, minus symbol) . (period, decimal point)/ (slash) 0 through 9; (semicolon)< (less than)= (equal sign)> (greater than) A through Z (upper case) a through z (lower case)

  7. Sorting • External Sorts - usually an operating system utility • Internal Sorts • Part of the COBOL standard

  8. Simple SORT syntax SORT file-name-1 ON {DESCENDING KEY { data-name-1} . . . }ASCENDING} USING {file-name-2} …. GIVING {fine-name-3} …. Simple form will read an input file sort it according to ON instructions write a sorted output file Program will then need to open file-name-2 and process a normal

  9. Sort Requirements • Need a Select Clause for Sort Work File • Sort Work File defined as an SD • Never - Never open or close SD or files defined in USING / GIVING • SD requires a record definition • Sort Key(s) are defined as part of the SD records • USING / GIVING objects • defined in a Select Clause and FD • SORT Controls the open and Close

  10. IDENTIFICATION DIVISION. PROGRAM-ID. SAMPLE-SORT. AUTHOR. ME. ENVIROMENT DIVISION. CONFIGURAION SECTION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT SALES-FILE ASSIGN TO 'A:MYINPUT.DAT' ORGANIZATION IS LINE SEQUENTIAL. SELECT SORT-FILE ASSIGN TO ‘DISK’. SELECT SORTED-SALES-FILE ASSIGN TO 'A:SLSSRTD.DAT' ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD SALES-FILE RECORD CONTAINS 39 CHARCACTERS. 01 SALES-RECORD PIC X(39). SD SORT-FILE RECORD CONTAINS 39 CHARACTERS. 01 SORT-RECORD. 05 PIC X(4). 05 SS-NUMBER PIC X(9). 05 SALES-PERSON PIC X(20). 05 PIC X(6). FD SORTED-SALES-FILE RECORD CONTAINS 39 CHARTACTERS. 01 SORTED-SALESPERSON-RECORD PIC X(39) PROCEDURE DIVISION. SORT SORT-FILE ON ASCENDING KEY SALES-PERSON ON ASCENDING KEY SS-NUMBER USING SALES-FILE GIVING SORTED-SALES-FILE STOP RUN. Sort Example

  11. Figure 14.6 Sorted Reports SALES ACTIVITY REPORT 04/21/03 PAGE 3 REGION LOCATION NAME ACCOUNT # SALES COMMISSION NORTHEAST TRENTON CLARK 576235 $ 100 $ 3 SOUTHEAST ST.PETERSBURG FEGEN 444444 $ 100 $ 2 SOUTHEAST ST.PETERSBURG FEGEN 476236 $ 376- $ 11- SOUTHEAST ST.PETERSBURG FEGEN 555555 $ 304 $ 15 SOUTHEAST ST.PETERSBURG JOHNSON 100000 $ 303- $ 18- SOUTHEAST ST.PETERSBURG JOHNSON 248545 $ 345 $ 48 SOUTHEAST ST.PETERSBURG JOHNSON 248545 $ 345 $ 57- SOUTHEAST ST.PETERSBURG JOHNSON 878787 $1,235 $ 148 $2,540 SALES ACTIVITY REPORT 04/21/03 PAGE 2 REGION LOCATION NAME ACCOUNT # SALES COMMISSION NORTHEAST BALTIMORE KARLSTROM 583645 $ 145 $ 6NORTHEAST BALTIMORE KARLSTROM 800396 $3,030 $ 273 NORTHEAST BALTIMORE MARCUS 700039 $ 932 $ 93 NORTHEAST BALTIMORE MARCUS 750020 $ 305 $ 15 NORTHEAST NEW YORK ADAMS 444333 $1,005- $ 10- NORTHEAST NEW YORK ADAMS 555666 $2,003 $ 401 NORTHEAST NEW YORK ADAMS 987654 $2,005 $ 201 NORTHEAST TRENTON CLARK 000101 $1,500 $ 150 NORTHEAST TRENTON CLARK 000104 $ 500 $ 15 NORTHEAST TRENTON CLARK 130101 $3,200 $ 640 SALES ACTIVITY REPORT 04/21/03 PAGE 1 REGION LOCATION NAME ACCOUNT # SALES COMMISSION MIDWEST CHICAGO BENWAY 000069 $ 231- $ 23-MIDWEST CHICAGO BENWAY 476530 $ 235- $ 12-MIDWEST CHICAGO BENWAY 988888 $ 450 $ 5MIDWEST CHICAGO BENWAY 999340 $ 334 $ 100MIDWEST CHICAGO HUMMER 000100 $ 107- $ 5-MIDWEST CHICAGO HUMMER 649355 $ 345 $ 17MIDWEST CHICAGO HUMMER 694446 $ 904 $ 90 MIDWEST ST. LOUIS HAAS 203000 $8,900 $ 445 MIDWEST ST. LOUIS HAAS 277333 $ 98- $ 8-MIDWEST ST. LOUIS HAAS 475365 $ 333 $ 17 (a) By Region, Location, and Name (All Records)

  12. Figure 14.7 Hierarchy Chart (USING/GIVING) Prepare a List of Salesperson Commissions in order by Region, Location, Name

  13. Commission Report Select --- Sales data in to be sorted Select --- Sorted Sales data out Select --- Sort Work File Select --- Report file

  14. Figure 14.8 Pseudocode (USING/GIVING) 100-prepare-commission-report Sort Sales File Using Unsorted-data Giving Sorted-sales Perform 200-houskeeping Perform Until No More Data Read Sorted Sales File At End Indicate No More Data Not At End Perform 210-process-sorted-records End-read End-perform Perform 220-finalize Stop 200-housekeeping Open Sorted Sales File, Print File Perform 300-get-todays-date220-finalize Perform 340-writetotals Close File 210-process-sorted-records Perform 320-calculate If New-page Required Perform 320-write-headers Else Perform 330-write-detail End-if300-get-todays-date Get Today’s Date 310-write-headers Write Heading Lines 320-calculate-commission Calculate Commission Increment Company Total330-write-detail Move Data To Detail-line Write Detail-line340-write-totals Write Totals-lines

  15. COMMISION REPORT PROCEDURE DIVISION. 100-SORT. SORT SORT-WORK-FILE ON ASCENDING KEY REGION LOCATION NAMEUSING SALES-DATA-FILE GIVING SORTED-SALES-DATA STOP.…. NOTE: SALES-DATA is read twice – BETTER to use an OUTPUT Procedure

  16. Figure 14.6 Sorted Reports – New Requirement SELECTED SALES ACTIVITY REPORT 04/21/03 PAGE 2 REGION LOCATION NAME ACCOUNT # SALES COMMISSION NORTHEAST TRENTON CLARK 130101 $3,200 $ 640MIDWEST ST. LOUIS HAAS 203000 $8,900 $ 445 NORTHEAST NEW YORK ADAMS 555666 $2,003 $ 401 NORTHEAST BALTIMORE KARLSTROM 800396 $3,030 $ 273 NORTHEAST NEW YORK ADAMS 987654 $2,005 $ 201 NORTHEAST TRENTON CLARK 000101 $1,500 $ 150SOUTHEAST ST.PETERSBURG JOHNSON 878787 $1,235 $ 148 *** COMPANY TOTAL = $ 21,873 $ 2,258 (b) By Decreasing Commission (Commission > $100) Note: Commission is not part of the input file

  17. Figure 14.10 Hierarchy Chart (INPUT PROCEDURE/OUTPUT PROCEDURE) Prepare a report of Salesperson Commission in descending Commission Order SORT SORTFILE ON DESCENDING KEY Commission INPUT PROCEDURE 200-Calculate-Commission OUTPUT PROCEDURE 1200-Prepare-Commission-Report

  18. Sort with Pre/Post Processing SORT SORTFILE ON ASCENDING / DESCENDING KEY INPUT PROCEDURE [PARAGRAPH / SECTION NAME ] OUTPUT PROCEDURE [PARAGRAPH / SECTION NAME ] 100-COMPUTE-SECTION. If you use sections you must exit through the Section Exit paragraph. This paragraph is the last thing in your SECTION and the only time you may validly use GO TO 900-SECTION.EXIT. EXIT.

  19. Input Procedure Processing • Open Input file • Read and pre process records as needed • Calculate a sort field • Select only certain records • Modify input in other ways • Send each record to the SORT • RELEASE SORT-REC [FROM …. ] • Do not OPEN or CLOSE the SORT-FILE

  20. Output Procedure Processing • Open OUTPUT File(s) and other INPUT files that might be needed • Retrieve records form the SORT-FILE • RETURN SORT-FILE …. AT END …. NOT AT END PERFORM Processing • END-RETURN • Do not open or close the SORT-FILE

  21. Figure 14.10 Hierarchy Chart (INPUT PROCEDURE/OUTPUT PROCEDURE) Prepare a report of Salesperson Commission in descending Commission Order SORT SORTFILE ON DESCENDING KEY Commission INPUT PROCEDURE 200-Calculate-Commission OUTPUT PROCEDURE 1200-Prepare-Commission-Report

  22. Commission Report 100-sort. Sort Sort-file On Descending Key Commission Input Procedure 200-calculate-commission Output Procedure 1210-prepare-report Stop Run. 200-calculate-commission. Perform 300-houskeeping Perform Until End-of-file = ‘Yes’ Read Sales-data-file At End Move ‘Yes’ To Eof Not At End Perform 310-process-input End-read End-perform. Perform 320-finalize 300-housekeeping Open Input Sales-data-file310-process-input Move In Put Data To Sort-rec Calculate Commission Move To Sort Record Release Sort-record320-finalize Close Sales-data-file

  23. Commission Report (con’t) SORT SORT-FILE ON DESCENDING KEY COMMISION INPUT PROCEDURE 200-CALCULATE-COMMISSION OUTPUT PROCEDURE 1210-PREPARE-REPORT STOP RUN. 200-CALCULATE-COMMISSION. ………. OMITTED ………….. 1210-PREPARE-REPORT PERFORM 1310-HOUSEKEEPING PERFORM UNTIL SORT-EOF = ‘YES’ RETURN SORT-FILE AT END MOVE ‘YES’ TO SORT-EOF NOT AT END PERFORM 1320-WRITE-REPORT END-RETURN END-PERFORM PERFROM 1330-FINALIZE STOP1310-HOUSEKEEPING OPEN OUTPUT REPORT-FILE1320-WRITE-REPORT MOVE DATA TO DETAIL LINE WRITE DETAIL-LINE1330-FINALIZE CLOSE REPORT-FILE.

  24. MERGE • Two or more files • Same data • Sorted in the same order • MERGE merge-file (defined in an SD) ON ASCENDING/DESCENDING KEY USING file-1 file-2 file-3 … (defined in FD)GIVING ……. OrOUTPUT Procedure…...

More Related