COBOL Bubble sort on linux

Here’s a sample COBOL bubble sort program for linux. You can simply save the source (.cbl file) on your computer then compile and link the code to an executable with the following command.

sudo cobc -free -x -o sort sort.cbl

IDENTIFICATION DIVISION.
        PROGRAM-ID. SORT.

       DATA DIVISION.
        WORKING-STORAGE SECTION.
           01 ARR OCCURS 5 TIMES PIC S9(2).
           01 TEMP PIC 999 VALUE 000.
           01 I PIC 9 VALUE 0.
           01 J PIC 9 VALUE 1.

       PROCEDURE DIVISION.
           DISPLAY "ENTER ANY FIVE NUMBERS:"
           PERFORM UNTIL I = 5
           ADD 1 TO I  
           ACCEPT ARR(I)
           END-PERFORM.

           MOVE 1 TO I.
           PERFORM UNTIL I > 5
           MOVE I TO J
           PERFORM UNTIL J > 5 
             IF (ARR(I) > ARR(J))
               MOVE ARR(I) TO TEMP
               MOVE ARR(J) TO ARR(I)
               MOVE TEMP TO ARR(J)
             END-IF
           ADD 1 TO J GIVING J
           END-PERFORM
           ADD 1 TO I GIVING I
           END-PERFORM.

           DISPLAY "AFTER SORTING:"
           MOVE 0 TO I.
           PERFORM UNTIL I = 5
           ADD 1 TO I  
           DISPLAY  I ":= " ARR(I)
           END-PERFORM.
       
        STOP RUN.

Execute the program by simply typing ./sort at the command prompt. It will prompt you to enter 5 numbers. The results are.

Download the sort.cbl file here

Leave a Reply

Your email address will not be published. Required fields are marked *