IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Sorted a COBOL array using Bubble sort


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
sreekusr
Warnings : 1

New User


Joined: 28 Aug 2006
Posts: 54
Location: Madrid

PostPosted: Wed Jul 04, 2007 10:36 am
Reply with quote

hi,

I have an array which occurs 5 times.
01 WS-ARRAY
02 WS-ARRAY1 occurs 5 times pic x(1).

values in array are
9
3
SPACE
1
4

Now i sorted that array using Bubble sort
after that i am getting the sorted array as follows
SPACE
1
3
4
9

but i want the space to be in last.i.e.
1
3
4
9
space

Please help me out in this

Thanks,
Sree
Back to top
View user's profile Send private message
murmohk1

Senior Member


Joined: 29 Jun 2006
Posts: 1436
Location: Bangalore,India

PostPosted: Wed Jul 04, 2007 10:46 am
Reply with quote

Sree,

The hexa value of space is X'40' whereas for the numeric data hexa starts at X'F0'. That is the reason you are getting the array as -

Code:

b (SPACE)
1
3
4
9
Back to top
View user's profile Send private message
agkshirsagar

Active Member


Joined: 27 Feb 2007
Posts: 691
Location: Earth

PostPosted: Wed Jul 04, 2007 4:16 pm
Reply with quote

Please see if this program meets your requirement. It uses alternate collating seqeunce "SORT-SEQ" so that 'space' comes after the numbers. Modify it as per your need.
Array-2 is your input array.

Good luck with your program. icon_smile.gif

Code:
IDENTIFICATION DIVISION.                     
PROGRAM-ID. TEST2.                           
ENVIRONMENT DIVISION.                         
CONFIGURATION SECTION.                       
OBJECT-COMPUTER. IBMMF.                       
SPECIAL-NAMES.                               
      ALPHABET SORT-SEQ IS "0123456789 ".   
INPUT-OUTPUT SECTION.                         
FILE-CONTROL.                                 
    SELECT SORT-FILE ASSIGN TO SORTFILE.     
DATA DIVISION.                               
FILE SECTION.                                 
SD SORT-FILE.                                 
01 ARRAY-1.                                   
   05 ELEMENT PIC X(01).                     
WORKING-STORAGE SECTION.                     
01 ARRAY-2 VALUE '93  654321'.               
   05 ELEMENT1 PIC X(01) OCCURS 10 TIMES.     
01 I PIC 99.                                             
PROCEDURE DIVISION.                                       
     DISPLAY 'IN THE PROGRAM'.                           
     SORT SORT-FILE ON ASCENDING KEY ELEMENT             
        COLLATING SEQUENCE IS SORT-SEQ                   
        INPUT PROCEDURE IS RELEASE-PARA                   
        OUTPUT PROCEDURE IS RETURN-PARA.                 
     DISPLAY SORT-RETURN.                                 
     DISPLAY 'SORTED ARRAY:' ARRAY-2.                     
     STOP RUN.                                           
RELEASE-PARA.                                             
     PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10           
       RELEASE ARRAY-1 FROM ELEMENT1(I)                   
     END-PERFORM.                                         
RETURN-PARA.                                             
     PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10           
       RETURN SORT-FILE RECORD  INTO ELEMENT1(I)         
        AT END DISPLAY 'SORT COMPLETED'                   
       END-RETURN                   
     END-PERFORM.
Back to top
View user's profile Send private message
stodolas

Active Member


Joined: 13 Jun 2007
Posts: 632
Location: Wisconsin

PostPosted: Wed Jul 04, 2007 8:18 pm
Reply with quote

Isn't an external sort more efficient than an internal bubble sort?
Back to top
View user's profile Send private message
agkshirsagar

Active Member


Joined: 27 Feb 2007
Posts: 691
Location: Earth

PostPosted: Wed Jul 04, 2007 10:46 pm
Reply with quote

stodolas wrote:
Isn't an external sort more efficient than an internal bubble sort?

This has been debated many times before, please search the topics to get more information.
By the way, if you read the original question more carefully, you will see that it is not about FILE sorting.. icon_cool.gif
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Jul 04, 2007 11:12 pm
Reply with quote

Hello,

The posted altseq may not work if the data contains other than the digits 0-9 and blank.

Should letters sort before or after numbers when they are part of the data? How should upper/lower case values be sorted?

Unless the number of values to be sorted is rather large, it will cost more system resources (cpu & i/o) to invoke the SORT than to just order the values in your code. The good news is that the SORT will always work and is easily modified if the sorting requirements change.
Back to top
View user's profile Send private message
agkshirsagar

Active Member


Joined: 27 Feb 2007
Posts: 691
Location: Earth

PostPosted: Thu Jul 05, 2007 11:30 am
Reply with quote

dick scherrer wrote:
The posted altseq may not work if the data contains other than the digits 0-9 and blank.

yes I am aware of that. icon_smile.gif
Please read my note for OP.
Quote:
Modify it as per your need.

I just tried to give direction towards the solution, as "special names" feature is seldomly used (Atleast in my shop) and many people don't know it actually.
Back to top
View user's profile Send private message
sreekusr
Warnings : 1

New User


Joined: 28 Aug 2006
Posts: 54
Location: Madrid

PostPosted: Thu Jul 05, 2007 3:04 pm
Reply with quote

Hi all,

Thanks a lot for the replies.


Thanks,
Sree
Back to top
View user's profile Send private message
Sandy Zimmer

Active Member


Joined: 13 Jun 2007
Posts: 826
Location: Wilmington, DE

PostPosted: Sat Jul 07, 2007 2:30 am
Reply with quote

If your tiny-tiny table will always only occur 5 times, or is very limited, why not use Shell-Metzner? You did not indicate how the table is loaded? When loading it, always check for spaces - if spaces, move high values to the occurence before the sort. BTW - you are using a lot of system overhead for that tiny little array.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Need to set RC4 through JCL SORT DFSORT/ICETOOL 5
No new posts How to split large record length file... DFSORT/ICETOOL 10
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
Search our Forums:

Back to Top