Bubble Sort (Ascending)

The PL/SQL code is based on C/C++ code described in [1] and [2]. The line 27 of the code determines the order of sorting (refer output diagram below), > for ascending.

DECLARE
  TYPE va_int IS VARRAY(10) OF PLS_INTEGER;
  v_int    va_int;
  v_val    PLS_INTEGER;
  v_tf    PLS_INTEGER := 1;
  v_idx    PLS_INTEGER := 0;
BEGIN
v_int := va_int(1,12,9,6,3,31,2,7,9);
 
  DBMS_OUTPUT.PUT('Input Array ['||v_int.COUNT||'] -> [');
  FOR idx IN v_int.FIRST..v_int.LAST LOOP
     IF idx < v_int.LAST THEN
        DBMS_OUTPUT.PUT(v_int(idx)||', ');
     ELSE
        DBMS_OUTPUT.PUT_LINE(v_int(idx)||']');
     END IF;
  END LOOP;
 
WHILE v_tf = 1 LOOP
  v_tf := 0;
  v_idx := v_idx+1;
  FOR idx IN v_int.FIRST..v_int.LAST LOOP
 
  IF idx < v_int.COUNT THEN
     IF v_int(idx) > v_int(idx+1) THEN
        v_val := v_int(idx);
        v_int(idx) := v_int(idx+1);
        v_int(idx+1) := v_val;
        v_tf := 1;
     END IF;
  END IF;
 
  END LOOP;
END LOOP;
  DBMS_OUTPUT.PUT_LINE(' Ascending Sort Iterations -> '||v_idx);
 
  DBMS_OUTPUT.PUT('Sorted Array ['||v_int.COUNT||'] -> [');
  FOR idx IN v_int.FIRST..v_int.LAST LOOP
     IF idx < v_int.LAST THEN
        DBMS_OUTPUT.PUT(v_int(idx)||', ');
     ELSE
        DBMS_OUTPUT.PUT_LINE(v_int(idx)||']');
     END IF;
  END LOOP;
 
END;
/

Bubble Sort (Descending)

The line 27 of the code determines the order of sorting (refer output diagram below), < for descending.

DECLARE
  TYPE va_int IS VARRAY(10) OF PLS_INTEGER;
  v_int    va_int;
  v_val    PLS_INTEGER;
  v_tf    PLS_INTEGER := 1;
  v_idx    PLS_INTEGER := 0;
BEGIN
v_int := va_int(1,12,9,6,3,31,2,7,9);
 
  DBMS_OUTPUT.PUT('Input Array ['||v_int.COUNT||'] -> [');
  FOR idx IN v_int.FIRST..v_int.LAST LOOP
     IF idx < v_int.LAST THEN
        DBMS_OUTPUT.PUT(v_int(idx)||', ');
     ELSE
        DBMS_OUTPUT.PUT_LINE(v_int(idx)||']');
     END IF;
  END LOOP;
 
WHILE v_tf = 1 LOOP
  v_tf := 0;
  v_idx := v_idx+1;
  FOR idx IN v_int.FIRST..v_int.LAST LOOP
 
  IF idx < v_int.COUNT THEN
     IF v_int(idx) < v_int(idx+1) THEN
        v_val := v_int(idx);
        v_int(idx) := v_int(idx+1);
        v_int(idx+1) := v_val;
        v_tf := 1;
     END IF;
  END IF;
 
  END LOOP;
END LOOP;
  DBMS_OUTPUT.PUT_LINE(' Descending Sort Iteration -> '||v_idx);
 
  DBMS_OUTPUT.PUT('Sorted Array ['||v_int.COUNT||'] -> [');
  FOR idx IN v_int.FIRST..v_int.LAST LOOP
     IF idx < v_int.LAST THEN
        DBMS_OUTPUT.PUT(v_int(idx)||', ');
     ELSE
        DBMS_OUTPUT.PUT_LINE(v_int(idx)||']');
     END IF;
  END LOOP;
 
END;
/

Reference Information

1. Robert Lafore, Object Oriented Programming in C++, Fourth Edition, 2001
2. Robert Lafore, The Waite Group's Turbo C Programming for the PC, Financial Times Prentice Hall; Rev Sub Edition 1988.

Back

  7609