Search code examples
algorithmbubble-sortcobol

COBOL Bubble Sort Only Sorting Last Element of Table


I am currently learning COBOL and I am trying to implement the Bubble Sort algorithm in my program. While I'm still very new to the language, what I have written makes sense to me semantically & syntactically, yet if I input 5, 4, 3, 2, & 1 in that order, my post sort table becomes 1, 5, 4, 3, 2. Could someone explain to me where I've gone wrong?

       IDENTIFICATION DIVISION.
         PROGRAM-ID. BubbleSort.

       DATA DIVISION.
         WORKING-STORAGE SECTION.
         01 TVAR PIC 9(4).
         01 CNT PIC 9(1) VALUE 1.
         01 CNT2 PIC 9(1) VALUE 1.
         01 ARR.
            05 ARRELEMENT PIC 9(4) OCCURS 5 TIMES.
         01 TABLELENGTH PIC 9(1) VALUE 5.

       PROCEDURE DIVISION.
         DISPLAY "Enter 5 numbers: ".
         PERFORM INPUT-PARA VARYING CNT FROM 1 BY 1 UNTIL CNT>5.

         DISPLAY "Pre Bubble-Sort: ".
         PERFORM PRINT-PARA VARYING CNT FROM 1 BY 1 UNTIL CNT>5.

         PERFORM BBLSORT-PARA.

         DISPLAY "Post Bubble-Sort: ".
         PERFORM PRINT-PARA VARYING CNT FROM 1 BY 1 UNTIL CNT>5.

         STOP RUN.

         INPUT-PARA.
            ACCEPT ARRELEMENT(CNT).

         PRINT-PARA.
            DISPLAY "Table element: "ARRELEMENT(CNT).

         BBLSORT-PARA.
             INITIALIZE CNT CNT2.
             MOVE 1 TO CNT.
             MOVE 2 TO CNT2.
             PERFORM UNTIL CNT>6
               PERFORM UNTIL CNT2>5
               DISPLAY "IF "ARRELEMENT(CNT) " IS > "ARRELEMENT(CNT2)
                 IF (ARRELEMENT((CNT)) > ARRELEMENT((CNT2)))
                 THEN
                   DISPLAY ARRELEMENT(CNT) " IS > "ARRELEMENT(CNT2)
                    MOVE ARRELEMENT(CNT) TO TVAR
                    MOVE ARRELEMENT(CNT2) TO ARRELEMENT(CNT)
                    MOVE TVAR TO ARRELEMENT(CNT2)
                 END-IF
                 DISPLAY "EXIT IF LOOP"
                 ADD 1 TO CNT2 GIVING CNT2
               END-PERFORM
               ADD 1 TO CNT GIVING CNT
             END-PERFORM.

       END PROGRAM BubbleSort.

Solution

  • Disclaimer: I am not a COBOL programmer, but I was able to make this work by these changes to the loop.

    PERFORM VARYING CNT
               FROM 1 BY 1
              UNTIL CNT > 4
    
       PERFORM VARYING CNT2
                  FROM 1 BY 1
                 UNTIL CNT2 + CNT > 5
    
          COMPUTE
             CNT3 = CNT2 + 1
          END-COMPUTE
    
          IF (ARRELEMENT((CNT2)) > ARRELEMENT((CNT3)))
             DISPLAY ARRELEMENT(CNT2) " IS > "ARRELEMENT(CNT3)
             MOVE ARRELEMENT(CNT3) TO TVAR
             MOVE ARRELEMENT(CNT2) TO ARRELEMENT(CNT3)
             MOVE TVAR TO ARRELEMENT(CNT2)
          END-IF
    
          DISPLAY "EXIT IF LOOP"
       END-PERFORM
    END-PERFORM.
    

    The key was to add a CNT3 which I used to stand for CNT2 + 1. Before the inner loop, it was necessary to resent CNT2 to 1 each time. And then the algorithm is to compare item CNT2 to item CNT2+1 within the loop each time without reference to CNT.

    Also, there was not a need to go all the way to the end of the array in the inner loop each time. I found it helpful to refer to Geeks for Geeks on bubble sort