SORTING A TABLE in COBOL

Hi folks, I could see the folds on you forehead. I can understand what you are thinking while reading the heading “Sorting table! its not required because we have sort utilities to do this”. But trust me there can be situations in CICS-COBOL program. Few days back one of my friend contacted me to get the code for sorting a table in Cobol, his requirement was very rare/unique so I had to write custom sort for his requirement, since he needs it in CICS-COBOL environment we could not use SORT verb. The reason why I said the story is, before started with writing the code I searched a couple of times in internet to see some sample codes; but internet disappointed me I couldn’t very much useful codes over there. I thought I can implement a simple “bubble sort” to sort the table.

How Bubble sort works?

The algorithm take elements from left most node to right most node and compare two adjacent elements and replace their positions if the right most element is greater than left most element of the pair.

lets take an example of  array {5 1 4 2 8}
The algorithm goes for n (length of array) n iterations across the array, and take one element in each phase
First Pass:
( 5 1 4 2 8 ) > ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them.
( 1 5 4 2 8 ) > ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) > ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) > ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
Second Pass:
( 1 4 2 5 8 ) > ( 1 4 2 5 8 )
( 1 4 2 5 8 ) > ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) > ( 1 2 4 5 8 )
( 1 2 4 5 8 ) > ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) > ( 1 2 4 5 8 )
( 1 2 4 5 8 ) > ( 1 2 4 5 8 )
( 1 2 4 5 8 ) > ( 1 2 4 5 8 )
( 1 2 4 5 8 ) > ( 1 2 4 5 8 )
the array is sorted, and the algorithm can terminate. Please see the below graphical illustration!.

Bubble-sort-example-300px (Image courtesy Wikipedia)

COBOL program for BUBBLE SORT (COBOL program for sorting an array)

 

IDENTIFICATION DIVISION.                                    
PROGRAM-ID. SORT01.                                         
AUTHOR.     SHIBU.T.                                        
DATA DIVISION.                                              
WORKING-STORAGE SECTION.                                    
01  TBL.                                                    
    02 WS-TBL OCCURS 10.                                    
       05  WS-FLD    PIC 99.                                
       05  WS-FLD1   PIC X(3).                              
       05  WS-FLD2   PIC 99.                                
01  WS-TAB-HLD.                                             
       05  WK-FLD    PIC 99.                                
       05  WK-FLD1   PIC X(3).                              
       05  WK-FLD2   PIC 99.                                
01  WS-I             PIC 99.                                
01  WS-J             PIC 99.                                
01  K                PIC 99.                                
PROCEDURE DIVISION.                                         
    MOVE ’01AAA25’    TO WS-TBL(1)                          
    MOVE ’01BBB
20’    TO WS-TBL(2)                          
    MOVE ’04CCC26’    TO WS-TBL(3)                          
    MOVE ’01DDD10’    TO WS-TBL(4)                          
    MOVE ’05EEE26’    TO WS-TBL(5)                          
    MOVE ’04FFF30’    TO WS-TBL(6)                          
    DISPLAY ‘>>>>>>>>BEFORE SORT<<<<<<<<‘                   
    PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 6         
      DISPLAY WS-TBL(WS-I)                                  
    END-PERFORM.                                            
    DISPLAY ‘>>>>>>>>ASCENDING ORDER<<<‘                    
    PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I = 7         
       PERFORM VARYING WS-J FROM WS-I BY 1 UNTIL WS-J > 6   
            IF WS-FLD(WS-J) < WS-FLD(WS-I) THEN             
                 MOVE WS-TBL(WS-I)  TO WS-TAB-HLD           
                 MOVE WS-TBL(WS-J)  TO WS-TBL(WS-I)         
                 MOVE WS-TAB-HLD TO WS-TBL(WS-J)            
            END-IF                                          
       END-PERFORM                                          
   END-PERFORM.                                              
   PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 6           
     DISPLAY WS-TBL(WS-I)                                    
   END-PERFORM.                                              
   DISPLAY ‘>>>>>>>>DESCENDING ORDER<<<<<<‘                  
   PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I = 7           
      PERFORM VARYING WS-J FROM WS-I BY 1 UNTIL WS-J > 6     
           IF WS-FLD(WS-J) > WS-FLD(WS-I) THEN               
                MOVE WS-TBL(WS-I)  TO WS-TAB-HLD             
                MOVE WS-TBL(WS-J)  TO WS-TBL(WS-I)           
                MOVE WS-TAB-HLD TO WS-TBL(WS-J)              
           END-IF                                            
      END-PERFORM                                            
   END-PERFORM.                                              
   PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 6           
     DISPLAY WS-TBL(WS-I)                                    
   END-PERFORM.                                              
   STOP RUN. 

IF WS-FLD(WS-J) < WS-FLD(WS-I) THEN  this line really defines the type of the sort, the above code makes sort to “sort in ascending order”. if you change the above line
to IF WS-FLD(WS-J) > WS-FLD(WS-I) THEN Sort will turn as a " Sort in Descending order"

Output.

Please note, we are taking only first two digits as key.

image

 

4 thoughts on “SORTING A TABLE in COBOL

  1. Hi,
    Excellent explanation.
    Although code is giving correct results but it is not using bubble sort technique.
    In Bubble Sort, 1st number is getting compared with second and then second with third and so on.
    But as per the logic of code written, it is comparing 1st number with all the remaining numbers and putting the smallest number at first place. Now it will pick second number and compare with rest of the numbers and so on.
    I am not sure which type of sort is this but definitely results are correct.

    Thank you.

Leave a Reply

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