1 chapter 14 - sorting system concepts –sort key major key (primary) minor key (secondary) –sort...
TRANSCRIPT
1
Chapter 14 - Sorting
• System Concepts– Sort key
• Major Key (Primary)• Minor Key (Secondary)
– Sort sequence• Ascending - Low to high• Descending – High to low
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
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
Figure 14.1 (b) Sorted Data, One Key
4
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
Figure 14.1 Sorted Data, Two Keys
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
– Collating Sequence (order of sorting)• Alphabetic fields
– Green vs. Greenfield
• Alphanumeric fields – 111 vs. AAA• EBCDIC sequence – IBM Mainframe• ASCII sequence – Most other computers
7
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)
Figure 14.2 EBCDIC & ASCII Collating Sequences
8
• COBOL Implementation– SORT command
• Syntax• INPUT PROCEDURE statement
– Selective sorting– Sorting on calculated fields
• USING statement• OUTPUT PROCEDURE statement
– Use of temporary work file
• GIVING statement– Retention of sorted data
9
SORT file-name-1
ON {DESCENDING KEY { data-name-1} . . . } ASCENDING}
WITH DUPLICATES IN ORDER
COLLATING SEQUENCE IS alphabet-name
INPUT PROCEDURE IS procedure-name-1USING {file-name-2}
OUTPUT PROCEDURE IS procedure-name-2GIVING {fine-name-3}
SORT syntax
10
• Uses 3 files to sort– Input file – contains unsorted records– Output file – contains sorted records– Sort file – temporary file used by COBOL to
sort the records
• First 2 must be defined in the FD section
• Sort file must be defined in a SD section
11
Input File Sort Output File
Sort file
12
Ex.) SD SALESPERSON-SORT-FILE01 SALESPERSON-SORT-RECORD
05 SP-REGION PIC 99.
05 SP-NUMBER PIC XXXX.
05 FILLER PIC X(52).
…
SORT SALESPERSON-SORT-FILE
ON ASCENDING KEY SP-REGION SP-NUMBER
USING SALESPERSON-INPUT-FILE
GIVING SALESPERSON-OUTPUT-FILE
13
SORT SALESPERSON-SORT-FILE
ON DESCENDING KEY SP-REGION
ON ASCENDING KEY SP-NUMBER
USING SALESPERSON-INPUT-FILE
GIVING SALESPERSON-OUTPUT-FILE
14
Assume that the records in a file named X-FILE are to be sorted descending sequence by X-FIELD. The records in X-FILE are 40 characters long. X-FIELD is located in positions 8-12 and is alphanumeric. The file sorted output is called Y-FILE
a. Code the SD entry and record description
b. Code the sort statement
c. Assume is should be sorted into ascending sequence by Y-FIELD and then into descending sequence by X-FIELD. Code the sort statements.
15
The registrar has asked for a simple report listing students by year, and alphabetically within year. Thus all freshmen are to appear first, then sophomores..The incoming record has the follow layout:
01 STUDENT-RECORD.
05 ST-NAME PIC X(15).
05 ST-MAJOR PIC X(15).
05 ST-YEAR PIC XX.
05 ST-CREDITS PIC X(10).
ST-YEAR USES FR,SO,JR AND GR. Develop the procedure division code to accomplish the desired sort.
16
• Merge statement