Page 1 of 1

Bubble Sort help!

Posted: Wed Jun 25, 2014 2:15 pm
by coco_xps
Hi. I have some trouble with this bubble sort. The sort is working but there are a lot of blank lines til line 1000. MAX-PERS has 1000 entries. How can I remove the blank lines?
Here is the code:

Code: Select all

**************************************************************
 WORKING-STORAGE SECTION.                                     
**************************************************************
*                                                             
********************                                          
* INPUT STRUCTURES *                                          
********************                                          
 01  INPUT-RECORD.                                            
     03  I-NAME                PIC X(30).                     
     03  I-PHONE               PIC X(20).                     
     03  FILLER                PIC X(30).                     
                                                              
*********************                                         
* OUTPUT STRUCTURES *                                         
*********************                                         
 01  OUTPUT-RECORD.                                           
     03 O-TEXT                 PIC X(80).                     
*                                                             
*********************                                         
* CONSTANTS         *                                         
*********************                                         
 01  SEARCH-NAME               PIC X(30) VALUE 'SEARCHNAME'.  
*                                                             
*********************                                         
* CONDITIONS        *                                         
*********************                                         
 01  EOF-Q01R6001-CONDITION    PIC S9(4) COMP VALUE ZERO.     
     88 EOF-Q01R6001                          VALUE 1.        
*                                                             
*********************                                         
* ARAYS             *                                         
*********************                                         
 01  PERS-TAB.                                                
     03 PERS-ELEM              OCCURS 1000.                   
        05 PERS-NAME           PIC X(30).                     
        05 PERS-PHONE          PIC X(20).                     
 01  IND-PERS                  PIC S9(4) COMP VALUE ZERO.     
 01  MAX-PERS                  PIC S9(4) COMP VALUE ZERO.     
 01  ANZ-PERS                  PIC S9(4) COMP VALUE ZERO.
*                                                 


******************************************************************
 1-A-HAUPTVERARB SECTION.                                         
           
******************************************************************
 1-A-HAUPTVERARB-ANF. .                                                           
*                                                                 
*--> BUBBLE SORT OF PERS-TAB <-----------------------------------*
*                                                                 
     PERFORM VARYING IND-PERS FROM 1 BY 1 UNTIL                   
                     IND-PERS = ANZ-PERS                          
        PERFORM VARYING ANZ-PERS FROM IND-PERS BY 1 UNTIL         
                        ANZ-PERS > MAX-PERS                       
             IF PERS-NAME &#40;ANZ-PERS&#41; < PERS-NAME &#40;IND-PERS&#41; THEN  
                MOVE PERS-ELEM &#40;ANZ-PERS&#41; TO INPUT-RECORD         
                MOVE PERS-ELEM &#40;IND-PERS&#41; TO PERS-ELEM &#40;ANZ-PERS&#41; 
                MOVE INPUT-RECORD TO PERS-ELEM &#40;IND-PERS&#41;         
             END-IF                                               
        END-PERFORM                                               
     END-PERFORM                                                  
     CONTINUE.                                                    
*                                                                 
 1-A-HAUPTVERARB-EX.                                                              
     EXIT.                                                        
/                       

****************************************************************
 2-A-VORLAUF SECTION.                                           
             
****************************************************************
 2-A-VORLAUF-ANF.                                                           
*                                                               
     MOVE        'P01B6000'              TO SYS-HPTPROG.        
     MOVE        'B'                     TO SYS-HPTPROGART.     
     MOVE        SPACE                   TO SYSERROR-BER.       
     MOVE        ZERO                    TO SYS-PROTNR.         
     SET         NO-SYSERROR             TO TRUE.               
*                                                               
     COMPUTE     MAX-PERS = LENGTH OF PERS-TAB /                
                            LENGTH OF PERS-ELEM.                
*                                                               
     OPEN        INPUT     Q01R6001.                            
     PERFORM     UNTIL EOF-Q01R6001                             
        READ     Q01R6001 INTO INPUT-RECORD                     
                 AT END SET EOF-Q01R6001 TO TRUE                
        END-READ                                                
        IF       NOT EOF-Q01R6001                               
           IF    ANZ-PERS LESS MAX-PERS                         
                 ADD 1                   TO ANZ-PERS            
                 MOVE INPUT-RECORD       TO PERS-ELEM &#40;ANZ-PERS&#41;
            ELSE                                                
                 SET      SYSERROR           TO TRUE            
                 MOVE     'A001'             TO SYS-PROTPKT     
                 MOVE     'OVERFL.INT.ARAY'  TO SYS-KURZMLDG    
                 PERFORM  P86U0002                              
           END-IF                                               
        END-IF                                                  
     END-PERFORM.                                               
     CLOSE       Q01R6001.                                      
*                                                               
 2-A-VORLAUF-EX.                                                                 
     EXIT.

Posted: Wed Jun 25, 2014 3:45 pm
by dhiraj
Can you give outfile(type of) what you got?

and you have OCCURS clause 1000 times.

Posted: Wed Jun 25, 2014 4:11 pm
by coco_xps
In the output I have on the last 12 lines names that are sorted. That means on lines 989 to 1000. The lines 1 to 989 are empty. The names are sorted ascending. If the sort is descending the names would be on the first 12 position and the rest of the lines would be empty. I need to remove the empty lines.
Someone told me that I use the maximum number of entries to control my loops instead of the actual number of entries containing data.
What should I change?

Posted: Wed Jun 25, 2014 6:41 pm
by William Collins

Code: Select all

PERFORM VARYING IND-PERS FROM 1 BY 1 UNTIL
    IND-PERS = ANZ-PERS
    PERFORM VARYING ANZ-PERS FROM IND-PERS BY 1 UNTIL
        ANZ-PERS > MAX-PERS 
You have this. So after your first iteration, ANZ-PERS is shot.

Don't use ANZ-PERS for more than one thing. It can only hold one value at any given time.

Posted: Wed Jun 25, 2014 6:46 pm
by coco_xps
What should I change? I don't really get it.

Posted: Wed Jun 25, 2014 8:24 pm
by William Collins
Your outer loop will be executed repeatedly for values of IND-PERS from 1 to the value of ANZ-PERS.

This you can read as "look at each entry in the table which contains data, because ANZ-PERS is the number of actual number of records read and stored".

So your outer loop is OK.

Your inner loop makes a mess of things, because it changes ANZ-PERS, so the next iteration of the outer loop is wrong, because ANZ-PERS has changed.

You need to create a new field to "vary" for the inner loop. You will still use ANZ-PERS to terminate the innner loop, but not in the way you are at the moment. Instead of stopping the inner loop at the end of the data, you are continuing to the end of the table.

You shouldn't need MAX-PERS at all to sort the table, just to load the table to ensure you don't overflow.

Both your PERFORM loops must use ANZ-PERS for termination, as in the value which decides that the limit of the table has been reached (not the value that is VARYING).

ANZ-PERS must not be changed after the data has been loaded into the table.

Posted: Thu Jun 26, 2014 5:54 pm
by coco_xps
How should I write that ?

Posted: Thu Jun 26, 2014 7:06 pm
by William Collins
Here and in the COBOL Cafe you've been given massive assistance with this. If you can't get it after all that, I'm not sure how you ever will.

So, if not a Troll, read everything, take the time to understand, and fix your code. It cannot be made any simpler for you.

Posted: Tue Dec 12, 2017 7:04 pm
by coco_xps
Please delete this whole post. Thanks.