|
Write
a programme in COBOL for sorting
an array using any sorting procedure.
Contributed by
Malisetty Siva Sankar
ALGORITHM :
Use of this method results in
the first part of the table being
sorted by first. By the bubble
sort-interchange method the first
step is that the second record
is compared with the first, and
if necessary they are interchanged.
Then the third record is compared
with second. If these two records
are interchanged the (new) second
record is compared with the first,
interchanging if necessary.
Next the fourth and third records
are compared for possible interchange.
Again if an interchange occurs,
we go " upward" and
compare the third with second
record and then possibly to find
its proper place, and hence the
name "bubble sort" which
is applied to this algorithm.
As an example of using the bubble
sort interchange algorithm, consider
the following set of six records
with sort key as indicated.
15
18
20
21
19
25
Comparison of the second and
first records results in no change.
Similarly comparison of the third
and second and of fourth and third
records results in no interchange.
However, when the fifth and fourth
records are compared (19 and 21)
an interchange results. Following
this comparison of fourth and
third (19 and 20 ) results in
an interchange, but then the comparison
of the third and second records
are compared (25 and 21. Since
his is last pair of records and
no interchange is required, thus
sort routine is completed.
CODE:
IDENTIFICATION DIVISION.
PROGRAM-ID.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 N PIC 999.
77 I PIC 999.
77 J PIC 999.
77 K PIC 999.
77 TEMP PIC 999.
77 D PIC X.
01 AAA.
02 A PIC 999 OCCURS 100 TIMES.
PROCEDURE DIVISION.
MAIN.
DISPLAY (1 1) ERASE.
DISPLAY (10, 5) "ENTER HOW
MANY NUMBER YOU WANT TO ENTER".
ACCEPT (10, 50) N WITH PROMPT.
DISPLAY (1 1) ERASE.
DISPLAY (6, 30) "INPUT THE
NUMBERS".
MOVE 6 TO LIN.
PERFORM ACC-PARA VARYING I FROM
1 BY 1 UNTIL I > N.
PERFORM BUB-PARA VARYING I FROM
1 BY 1 UNTIL I > N.
DISPLAY (1 1) ERASE.
DISPLAY (5, 30) "OUTPUT (BUBBLE
SORT)".
DISPLAY (6, 30) "~~~~~~ ~~~~~~
~~~~".
MOVE 6 TO LIN.
PERFORM DIS-PARA VARYING I FROM
1 BY 1 UNTIL I > N.
STOP RUN.
ACC-PARA.
COMPUTE LIN = LIN + 1.
ACCEPT (LIN, 35) A(I) WITH PROMPT.
IF LIN = 20
DISPLAY (1 1) ERASE
DISPLAY (6, 30) "CONTINUE"
MOVE 6 TO LIN.
BUB-PARA.
COMPUTE K = I + 1.
PERFORM BUB1-PARA VARYING J FROM
K BY 1 UNTIL J > N.
BUB1-PARA.
IF A(I) > A(J)
MOVE A(I) TO TEMP
MOVE A(J) TO A(I)
MOVE TEMP TO A(J).
DIS-PARA.
COMPUTE LIN = LIN + 1.
DISPLAY (LIN, 33) A(I).
IF LIN = 20
DISPLAY (24, 30) "PRESS ANY
KEY"
ACCEPT D
DISPLAY (1 1) ERASE
MOVE 6 TO LIN.
Output
MAIN MENU
ENTER HOW MANY NUMBER YOU WANT
TO ENTER…….6
SUBMENU
INPUT
THE NUMBERS
15
18
20
21
19
25
OUTPUT (BUBBLE SORT)
-----------------------------------
15
18
19
20
21
25
|

|