sorting an array

5 pts.
Tags:
Arrays
COBOL/400
Can you sort an array in cobol AS400?

Answer Wiki

Thanks. We'll let you know when a new response is added.

From

<a href=”http://en.wikibooks.org/wiki/Algorithm_implementation/Sorting/Bubble_sort”>http://en.wikibooks.org/wiki/Algorithm_implementation/Sorting/Bubble_sort</a>

WORKING-STORAGE SECTION.
*
01 WS-SORT-AREA.
05 WS-SORT-TABLE.
10 WS-SORT-ROW PIC X(a) OCCURS b.
05 WS-TEMP-ROW PIC X(a).
05 WS-ROW-MAX PIC S9(4) COMP VALUE b.
05 WS-SORT-MAX PIC S9(4) COMP.
05 WS-SORT-UP PIC S9(4) COMP.
05 WS-SORT-DOWN PIC S9(4) COMP.
05 WS-SORT-INCR PIC S9(4) COMP.
05 WS-SORT-TEST PIC S9(4) COMP.
*
PROCEDURE DIVISION.
*
MY-SORT SECTION.
MY-SORT-START.
*
* find the last entry
*
PERFORM VARYING WS-SORT-MAX FROM WS-ROW-MAX BY -1
UNTIL WS-SORT-MAX = ZERO
OR WS-SORT-ROW (WS-SORT-MAX) NOT = SPACES
END-PERFORM.
*
* bubble sort into required sequence
*
PERFORM VARYING WS-SORT-UP FROM WS-SORT-MAX BY -1
UNTIL WS-SORT-UP = ZERO
*
MOVE ZERO TO WS-SORT-TEST
*
PERFORM VARYING WS-SORT-DOWN FROM 1 BY 1
UNTIL WS-SORT-DOWN = WS-SORT-UP
*
ADD 1 TO WS-SORT-DOWN GIVING WS-SORT-INCR
*
IF WS-SORT-ROW (W30-SORT-DOWN)
> WS-SORT-ROW (W30-SORT-INCR)
*
MOVE WS-SORT-ROW (WS-SORT-DOWN)
TO WS-TEMP-ROW
MOVE WS-SORT-ROW (WS-SORT-INCR)
TO WS-SORT-ROW (WS-SORT-DOWN)
MOVE WS-TEMP-ROW
TO WS-SORT-ROW (WS-SORT-INCR)
ADD 1 TO WS-SORT-TEST
END-IF
END-PERFORM
*
IF WS-SORT-TEST = ZERO
NEXT SENTENCE
END-IF
END-PERFORM.
*
MY-SORT-EXIT.
EXIT.

Discuss This Question: 4  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Sloopy
    There is an excellent article on sort algorithms in COBOL here, including full code samples and timings for AS/400 : http://home.att.net/~arnold.trembley/svalgard.htm Regards, Sloopy
    2,195 pointsBadges:
    report
  • Cwc
    Thanks for the link, Sloopy - quite interesting.
    4,290 pointsBadges:
    report
  • SbElectric
    Excellent reference materrial on SORT. But do consider utilizing external SORT (or Syncsort) utilities, if possible. Sometimes it is easirer & convenient ... then let the SORT output feed into the COBOL program.
    2,540 pointsBadges:
    report
  • Cwc
    There is also the qsort (quick sort) function in the C runtime library that can be bound into an ILE program. If a quick sort meets your needs, you wouldn't have to write the logic in COBOL; you could let qsort do the work. You'd just need to code a comparison method for it to use.
    4,290 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following