Saturday, 1 March 2014

COBOL program to implement Numerical Sorting.



COBOL program to implement Numerical Sorting.

PROGRAM:

       IDENTIFICATION DIVISION.
       PROGRAM-ID SORTING.
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
        01 ARRAY.
           02 A1 PIC 9(4) OCCURS 5 TIMES.
           77 I PIC 9(3).
           77 J PIC 9(3).
           77 K PIC 9(3).
           77 N PIC 9(3).
           77 TEMP PIC 9(4)V99.
           77 NUM PIC 9(3).
           77 NUM1 PIC 9(3).
           77 DISP PIC 9(3).
       PROCEDURE DIVISION.
       MAIN-PARA.
           DISPLAY " ENTER THE TOTAL NUMBER OF ELEMENTS TO SORT :".
           ACCEPT N.
           DISPLAY " THE VALUE OF N IS :".
           DISPLAY " ".
           DISPLAY " ENTER NUMBER TO BE SORTED".
           DISPLAY " ".
           PERFORM ACC-PARA VARYING I FROM 1 BY 1 UNTIL I>N.
       SORT-PARA.
           PERFORM BUBBLEPARA VARYING I FROM 1 BY 1 UNTIL I>N.
           DISPLAY " THE SORTED ELEMENTS ARE:".
           DISPLAY " ".
           PERFORM DISP-PARA VARYING I FROM 1 BY 1 UNTIL I>N.
           DISPLAY " ".
           STOP RUN.
       ACC-PARA.
           DISPLAY " ENTER THE ELEMENT NUMBER", I.
           ACCEPT A1 (I).
       DISP-PARA.
           MOVE A1 (I) TO DISP.
           DISPLAY DISP.
       BUBBLEPARA.
           SUBTRACT I FROM N GIVING K.
           PERFORM COMPARE-PARA VARYING J FROM 1 BY 1 UNTIL J>K.
       COMPARE-PARA.
           COMPUTE NUM = J.
           COMPUTE NUM1 = NUM + 1.
           IF A1 (NUM) IS GREATER THAN A1 (NUM1)
           MOVE A1 (NUM) TO TEMP
           MOVE A1 (NUM1) TO A1 (NUM)
           MOVE TEMP TO A1 (NUM1).
        

No comments: