ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

COBOL Bubble Sort with Optimization

COBOL

Goal -- WPM

Ready
Exercise Algorithm Area
1IDENTIFICATION DIVISION.
2PROGRAM-ID. BUBBLE-SORT-OPTIMIZED.
3AUTHOR. Admin.
4
5DATA DIVISION.
6WORKING-STORAGE SECTION.
701 NUMBERS-TABLE.
805 NUMBERS OCCURS 10 TIMES
9INDEXED BY IDX-NUMBERS
10PIC 9(4) VALUE 0.
11
1201 SWAPPED-FLAG PIC X VALUE 'N'.
1388 SWAPPED VALUE 'Y'.
1488 NOT-SWAPPED VALUE 'N'.
15
1601 I PIC 9(2) VALUE 0.
1701 J PIC 9(2) VALUE 0.
1801 TEMP-VALUE PIC 9(4) VALUE 0.
1901 N PIC 9(2) VALUE 0.
20
21PROCEDURE DIVISION.
22MAIN-LOGIC.
23MOVE 7 TO N.
24MOVE 5210, 12, 345, 6, 7890, 1, 999 TO NUMBERS(1) THRU NUMBERS(N).
25
26DISPLAY "Original Array:".
27PERFORM DISPLAY-ARRAY.
28
29PERFORM BUBBLE-SORT-OPTIMIZED.
30
31DISPLAY "Sorted Array:".
32PERFORM DISPLAY-ARRAY.
33
34STOP RUN.
35
36BUBBLE-SORT-OPTIMIZED.
37MOVE 'Y' TO SWAPPED-FLAG.
38SUBTRACT 1 FROM N.
39PERFORM VARYING I FROM 1 BY 1 UNTIL I > N
40MOVE 'N' TO SWAPPED-FLAG
41PERFORM VARYING J FROM 1 BY 1 UNTIL J > N - I
42IF NUMBERS(J) > NUMBERS(J + 1)
43PERFORM SWAP-ELEMENTS
44MOVE 'Y' TO SWAPPED-FLAG
45END-IF
46END-PERFORM
47IF NOT-SWAPPED
48EXIT PERFORM
49END-IF
50END-PERFORM.
51
52SWAP-ELEMENTS.
53MOVE NUMBERS(J) TO TEMP-VALUE.
54MOVE NUMBERS(J + 1) TO NUMBERS(J).
55MOVE TEMP-VALUE TO NUMBERS(J + 1).
56
57DISPLAY-ARRAY.
58PERFORM VARYING IDX-NUMBERS FROM 1 BY 1 UNTIL IDX-NUMBERS > N
59DISPLAY NUMBERS(IDX-NUMBERS)
60END-PERFORM.
Algorithm description viewbox

COBOL Bubble Sort with Optimization

Algorithm description:

This COBOL program implements an optimized Bubble Sort algorithm to sort an array of integers in ascending order. It includes a flag to detect if any swaps occurred in a pass, allowing for early termination if the array becomes sorted before all passes are completed. This is a fundamental sorting algorithm often used for educational purposes to understand basic sorting principles and loop control.

Algorithm explanation:

The Bubble Sort algorithm repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The optimization implemented here uses a `SWAPPED-FLAG` to check if any swaps were made during a pass. If no swaps occur, it means the array is already sorted, and the algorithm terminates early. The outer loop runs at most N-1 times, and the inner loop compares adjacent elements. In the worst case, where the array is in reverse order, the time complexity is O(N^2). In the best case, with the optimization, if the array is already sorted, it's O(N). The space complexity is O(1) as it only uses a few extra variables for swapping and control. Edge cases handled include an empty array (N=0, loops won't execute) and a single-element array (N=1, loops won't execute). The algorithm guarantees correctness because each pass moves the largest unsorted element to its correct final position.

Pseudocode:

PROCEDURE BUBBLE-SORT-OPTIMIZED:
  SET SWAPPED-FLAG to TRUE
  DECREMENT N by 1
  LOOP I from 1 to N:
    SET SWAPPED-FLAG to FALSE
    LOOP J from 1 to N - I:
      IF element at J > element at J+1:
        SWAP elements at J and J+1
        SET SWAPPED-FLAG to TRUE
      END IF
    END LOOP
    IF SWAPPED-FLAG is FALSE:
      EXIT LOOP
    END IF
  END LOOP
END PROCEDURE