用cobol写个冒泡排序
时间: 2023-04-03 20:05:31 浏览: 176
用C语言编写的冒泡排序法
4星 · 用户满意度95%
冒泡排序是一种简单的排序算法,可以用 COBOL 语言实现。以下是 COBOL 冒泡排序的示例代码:
IDENTIFICATION DIVISION.
PROGRAM-ID. BUBBLE-SORT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ARRAY-SIZE PIC 9(4) VALUE 10.
01 NUMBERS OCCURS 0 TO 100 TIMES DEPENDING ON ARRAY-SIZE PIC 9(4).
01 TEMP PIC 9(4).
01 I PIC 9(4).
01 J PIC 9(4).
PROCEDURE DIVISION.
MAIN-LOGIC.
DISPLAY "Enter the size of the array (maximum 100): ".
ACCEPT ARRAY-SIZE.
DISPLAY "Enter the numbers to be sorted: ".
PERFORM READ-INPUT.
PERFORM BUBBLE-SORT.
DISPLAY "Sorted numbers: ".
PERFORM DISPLAY-OUTPUT.
STOP RUN.
READ-INPUT.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > ARRAY-SIZE
DISPLAY "Number " I ": ".
ACCEPT NUMBERS(I)
END-PERFORM.
BUBBLE-SORT.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > ARRAY-SIZE - 1
PERFORM VARYING J FROM 1 BY 1 UNTIL J > ARRAY-SIZE - I
IF NUMBERS(J) > NUMBERS(J+1)
MOVE NUMBERS(J) TO TEMP
MOVE NUMBERS(J+1) TO NUMBERS(J)
MOVE TEMP TO NUMBERS(J+1)
END-IF
END-PERFORM
END-PERFORM.
DISPLAY-OUTPUT.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > ARRAY-SIZE
DISPLAY NUMBERS(I)
END-PERFORM.
以上代码演示了如何使用 COBOL 语言实现冒泡排序。
阅读全文