Advertisement
Miquel_Fuster

Ordenamiento por selección

Dec 7th, 2021
3,484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
COBOL 1.44 KB | None | 0 0
  1. IDENTIFICATION DIVISION.
  2. PROGRAM-ID. ORDENAMIENTO-POR-SELECCION.
  3.  
  4. DATA DIVISION.
  5. WORKING-STORAGE SECTION.
  6. 01 TIMESTAMP.
  7.     02 FILLER   PIC 9(06).
  8.     02 MS       PIC 9(02).
  9.    
  10. 77 TABLA        PIC 9(02) OCCURS 20 INDEXED BY I.
  11. 77 AUX          PIC 9(02).
  12. 77 J            PIC 9(02).
  13.  
  14. PROCEDURE DIVISION.
  15. MAIN-PROCEDURE.
  16.     PERFORM RELLENAR-TABLA.
  17.     DISPLAY "TABLA ORIGINAL".
  18.     PERFORM MOSTRAR-TABLA.
  19.     PERFORM ORDENAR-TABLA.
  20.     DISPLAY "TABLA ORDENADA".
  21.     PERFORM MOSTRAR-TABLA.
  22.     STOP RUN.
  23.    
  24. MOSTRAR-TABLA.
  25.     PERFORM VARYING I FROM 1 BY 1 UNTIL I > 19
  26.         DISPLAY TABLA(I), ", " WITH NO ADVANCING
  27.     END-PERFORM.
  28.     DISPLAY TABLA(I).
  29.    
  30. RELLENAR-TABLA.
  31.     COMPUTE TABLA(1) = FUNCTION RANDOM(FUNCTION SECONDS-PAST-MIDNIGHT) * 99 + 1.
  32.     PERFORM VARYING I FROM 2 BY 1 UNTIL I > 20
  33.         COMPUTE TABLA(I) = FUNCTION RANDOM * 99 + 1
  34.     END-PERFORM.
  35.  
  36. ORDENAR-TABLA.
  37.     PERFORM VARYING I FROM 1 BY 1 UNTIL I > 20
  38.         MOVE I TO AUX
  39.         PERFORM VARYING J FROM I BY 1 UNTIL J > 20
  40.             IF TABLA(AUX) > TABLA(J) THEN
  41.                 MOVE J TO AUX
  42.             END-IF
  43.         END-PERFORM
  44.         *> REUTILIZACION DE AUX. AQUÍ SE PUEDE. EN OTRO CONTEXTO TAL VEZ SERIA
  45.         *> NECESARIA UNA VARIABLE AUXILIAR
  46.         MOVE AUX TO J
  47.         MOVE TABLA(I) TO AUX
  48.         MOVE TABLA(J) TO TABLA(I)
  49.         MOVE AUX TO TABLA(J)
  50.     END-PERFORM.
  51.        
  52. END PROGRAM ORDENAMIENTO-POR-SELECCION.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement