Miquel_Fuster

qsort - subprograma

Jan 11th, 2022 (edited)
991
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
COBOL 1.30 KB | None | 0 0
  1. IDENTIFICATION DIVISION.
  2. PROGRAM-ID. QSORT RECURSIVE.
  3. DATA DIVISION.
  4.  
  5. LOCAL-STORAGE SECTION.
  6. 01 I-IZ     INDEX.
  7. 01 I-DE     INDEX.
  8.  
  9. 01 A        INDEX.
  10. 01 B        INDEX.
  11.  
  12. 01 AUX      PIC 9.
  13.  
  14. LINKAGE SECTION.
  15. 01 PRIMERO  INDEX.
  16. 01 ULTIMO   INDEX.
  17.  
  18. 01 ARRAY.
  19.     05 ELEMENTO PIC 9 OCCURS 10.
  20.    
  21. PROCEDURE DIVISION USING BY REFERENCE ARRAY, PRIMERO, ULTIMO.
  22. MAIN-PROCEDURE.
  23.     SET I-IZ TO PRIMERO.
  24.     SET I-DE TO ULTIMO.
  25.    
  26.     IF PRIMERO < ULTIMO
  27.         PERFORM UNTIL I-IZ NOT < I-DE
  28.             PERFORM UNTIL ELEMENTO(I-IZ) NOT < ELEMENTO(ULTIMO) OR I-IZ = I-DE
  29.                 SET I-IZ UP BY 1
  30.             END-PERFORM
  31.            
  32.             PERFORM UNTIL ELEMENTO(I-DE) NOT > ELEMENTO(ULTIMO) OR I-IZ = I-DE
  33.                 SET I-DE DOWN BY 1
  34.             END-PERFORM
  35.            
  36.             SET A TO I-IZ
  37.             SET B TO I-DE
  38.             PERFORM SWAP
  39.         END-PERFORM
  40.        
  41.         SET A TO I-IZ
  42.         SET B TO ULTIMO
  43.         PERFORM SWAP
  44.        
  45.         SET I-IZ DOWN BY 1
  46.         SET I-DE UP   BY 1
  47.         CALL 'QSORT' USING ARRAY, PRIMERO, I-IZ
  48.         CALL 'QSORT' USING ARRAY, I-DE, ULTIMO
  49.     END-IF.
  50.     GOBACK.
  51.  
  52. SWAP.
  53.     MOVE ELEMENTO(A) TO AUX.
  54.     MOVE ELEMENTO(B) TO ELEMENTO(A).
  55.     MOVE AUX TO ELEMENTO(B).
  56.    
  57. END PROGRAM QSORT.
  58.  
Add Comment
Please, Sign In to add comment