## How to Implement Bubble sort in Cobol ??

Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

### Re: How to Implement Bubble sort in Cobol ??

Perhaps the real requirement is to determine a median value, which often implies a sort.
steve-myers
Global moderator

Posts: 1771
Joined: Thu Jun 03, 2010 6:21 pm
Has thanked: 4 times
Been thanked: 167 times

### Re: How to Implement Bubble sort in Cobol ??

Hi Steve,

Possibly - we're kinda limited when so little usable info is posted. . .

d

dick scherrer
Global moderator

Posts: 6308
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 90 times

### Re: How to Implement Bubble sort in Cobol ??

Agreed. Until I saw the OP was "stuck" after he had gotten the average, so the next usable statistic - often the more meaningful statistic in many circumstances - is a median.
steve-myers
Global moderator

Posts: 1771
Joined: Thu Jun 03, 2010 6:21 pm
Has thanked: 4 times
Been thanked: 167 times

### Re: How to Implement Bubble sort in Cobol ??

Sorry for not providing info .

Code to Find total :

5000-TOTAL-RTN.

MOVE 0 TO WS-I
MOVE ID-NO-IN TO ID-NO-OUT
MOVE STUDENT-NAME-IN TO STUDENT-NAME-OUT
MOVE EXAM1 TO EXAM1-OUT
MOVE EXAM2 TO EXAM2-OUT
MOVE EXAM3 TO EXAM3-OUT
ADD EXAM1 EXAM2 EXAM3 GIVING WS-AVERAGE(WS-I)
DIVIDE 3 INTO WS-AVERAGE(WS-I) ROUNDED
IF WS-AVERAGE(WS-I) IS GREATER THAN 060
PERFORM 5001-PASS-RTN
ELSE
PERFORM 5002-FAIL-RTN
END-IF.

Code to find Rank which i have tried :

5003-RANK-CAL.

PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I= 5
PERFORM VARYING WS-Y FROM WS-I BY 1 UNTIL WS-Y>5
IF WS-AVERAGE(WS-Y) < WS-AVERAGE(WS-I)
MOVE WS-AVERAGE(WS-I) TO WS-TEMP
MOVE WS-AVERAGE(WS-Y) TO WS-AVERAGE(WS-I)
MOVE WS-TEMP TO WS-AVERAGE(WS-Y)
END-IF
END-PERFORM
END-PERFORM.

Working Storage Section :

01 WS-DETAIL-LINE.
05 FILLER                        PIC X(3) VALUE SPACES.
05 ID-NO-OUT                  PIC X(5).
05 FILLER                        PIC X(5) VALUE SPACES.
05 STUDENT-NAME-OUT    PIC X(20).
05 EXAM1-OUT                 PIC X(5).
05 FILLER                        PIC X(2) VALUE SPACES.
05 EXAM2-OUT                 PIC X(5).
05 FILLER                        PIC X(3) VALUE SPACES.
05 EXAM3-OUT                  IC X(5).
05 FILLER                     PIC X(3) VALUE SPACES.
05 WS-AVER1                   PIC 9(3).
05                            PIC X(9) VALUE SPACES.
05 STATUS-OUT                 PIC X.

01 WS-AVER.
10 WS-AVERAGE  OCCURS 5 TIMES INDEXED BY WS-I
PIC 9(3).
gauthamnagpur18

Posts: 93
Joined: Sat Oct 23, 2010 1:28 pm
Location: Chennai, India
Has thanked: 3 times
Been thanked: 2 times

### Re: How to Implement Bubble sort in Cobol ??

Hello,

It will help us help you if you post some sample "input" data and the result(s) you want when that sample data is processed by your code.
Hope this helps,
d.sch.

dick scherrer
Global moderator

Posts: 6308
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 90 times

### Re: How to Implement Bubble sort in Cobol ??

Hi ,

Input :

Student-ID Student-Name Mark 1 Mark2 Mark3
00000001 HArris 58 52 90
00000002 Raam 100 99 60
00000003 Chris 67 100 70

O/p :

Student-ID Student-Name Mark 1 Mark2 Mark3 Average Rank
00000002 Raam 100 99 60 86 1
00000003 Chris 67 100 70 79 2
00000001 HArris 58 52 90 66 3

Based on average , Rank is alloted.

Thanks
gauthamnagpur18

Posts: 93
Joined: Sat Oct 23, 2010 1:28 pm
Location: Chennai, India
Has thanked: 3 times
Been thanked: 2 times

### Re: How to Implement Bubble sort in Cobol ??

Hello,

Ok - i believe we are all on the same page now

Are you still stuck?

If you have all of the info in an array and have calculated the average (and put this into the array also), you are ready to implement the bubble sort to get the results into the desired sequence. Keep in mind that most of these sorts sort from low to high but you want to sort descending (high to low).

If the descending sort causes problems, suggest you sort ascendng and then process the array from the bottom afgter the sort.
Hope this helps,
d.sch.

dick scherrer
Global moderator

Posts: 6308
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 90 times

### Re: How to Implement Bubble sort in Cobol ??

Ya i really got stuck. I have found out average for all student-ids but there is problem in finding the rank .Actually I don know how to use table concept and Implement bubble sort here .
gauthamnagpur18

Posts: 93
Joined: Sat Oct 23, 2010 1:28 pm
Location: Chennai, India
Has thanked: 3 times
Been thanked: 2 times

### Re: How to Implement Bubble sort in Cobol ??

gauthamnagpur18 wrote:Ya i really got stuck. I have found out average for all student-ids but there is problem in finding the rank .Actually I don know how to use table concept and Implement bubble sort here .

Deary, deary, me

Do you understand the concept of a working-storage variable? Do understand that you may define many such variables?

Then consider that a COBOL table (or any sort of array implementation in any language) is a number of working-storage variables, all of which have the same name, but which may be differentiated by the usage of an index or a subscript (different things in COBOL, although not in most other languages).

Thus, you might write:
01  WS-VARIABLE-1        PIC X(10).
01  WS-VARIABLE-2        PIC X(10).
:
:
01  WS-VARIABLE-100      PIC X(10).

But if you had some intelligence and education, you'd understand that you were much better off writing:
01  WS-TABLE OCCURS 100 TIMES INDEXED BY WS-TABLE-INDEX PIC X(10).

Since in the first case, you'd have to process the variables like:
MOVE WS-SOMETHING TO WS-VARIABLE-1.
MOVE WS-SOMETHING TO WS-VARIABLE-2.
:
:
MOVE WS-SOMETHING TO WS-VARIABLE-100.

Whereas in the second you could write:
PERFORM MOVE-TO-ELEMENT
VARYING WS-TABLE-INDEX FROM 1 UNTIL WS-TABLE-INDEX > 100.
:
:
MOVE-TO-ELEMENT.

MOVE WS-SOMETHING TO WS-TABLE(WS-TABLE-INDEX).

Questions?
"You have sat too long for any good you have been doing lately ... Depart, I say; and let us have done with you. In the name of God, go!" -- what I say to a junior programmer at least once a day

Akatsukami
Global moderator

Posts: 995
Joined: Sat Oct 16, 2010 2:31 am
Location: Bloomington, IL
Has thanked: 6 times
Been thanked: 48 times

### Re: How to Implement Bubble sort in Cobol ??

Hi
I am still struggling to get output . Pls tell me what to do ??? Till average calculation , I have completed ! Next Rank Calculation is little bit tuff for me !!

Thanks
Gautham
gauthamnagpur18

Posts: 93
Joined: Sat Oct 23, 2010 1:28 pm
Location: Chennai, India
Has thanked: 3 times
Been thanked: 2 times

PreviousNext

• Related topics
Replies
Views
Last post