#BUBBLE_SORT
-- This test shows use of traditional list and
--  of numeric table .
-- Executes bubble sorting of list of numbers
-- Used interactively and through parameters
$B:=#PARM(T);
OPEN S ' ';
$MEMORY:=$B[1];
IF NOT $MEMORY ->
  LOOP
    $MEMORY:=#CALL_PAS(1 'Enter size of the table {elements, <=total');
               --     1=WRITE/READ
    IF $MEMORY>0 -> BREAK FI;
  END;
FI;

$SIZE:=$B[2];
IF NOT $SIZE ->
  LOOP
    $SIZE:=#CALL_PAS(1 'Enter count of sorted numbers');
                  --    1=WRITE/READ
    IF $SIZE>0 -> BREAK FI;
  END;
FI;

$TAB:=#CALL_PAS(60 $MEMORY); -- create
IF $TAB<=0 -> #CALL_PAS(1 'STOP, TABLE NOT CREATED');FI;
$I:=1;
LOOP
  $RAND:=#CALL_PAS(20 100); -- random
  $LIST!.:=$RAND;
  #CALL_PAS(62 $TAB $I $RAND); -- put
  IF $I=$SIZE->BREAK FI;
  $I+:=1;
END;
#CALL_PAS(30 '  Starts work with list ');
PRINT $LIST;
$FLAG:=T;
LOOP
IF NOT $FLAG->BREAK FI;
  $FLAG:=NULL;
  $I:=1;
  LOOP
   IF $LIST[$I]>$LIST[$I+1] ->
        $M:=$LIST[$I];
        $LIST[$I]:=$LIST[$I+1];
        $LIST[$I+1]:=$M;
        $FLAG:=T;
    FI;
    IF $I=$SIZE-1->BREAK FI;
    $I+:=1;
  END;
END;


PRINT $LIST;
#CALL_PAS(30 ' Starts work with table  ');
PRINT #CALL_PAS(65 $TAB);
$FLAG:=T;
LOOP
IF NOT $FLAG->BREAK FI;
  $FLAG:=NULL;
  $I:=1;
  LOOP
   IF #CALL_PAS(63 $TAB $I)>#CALL_PAS(63 $TAB $I+1) ->
        $M:=#CALL_PAS(63 $TAB $I);
        #CALL_PAS(62 $TAB $I #CALL_PAS(63 $TAB $I+1));
        #CALL_PAS(62 $TAB $I+1 $M);
        $FLAG:=T;
    FI;
    IF $I=$SIZE-1->BREAK FI;
    $I+:=1;
  END;
END;
IF #CALL_PAS(65 $TAB)=$LIST ->
   S<<RESULTS ARE EQUAL
ELSIF T->
   S<<RESULTS ARE DIFFERENT
FI;
PRINT #CALL_PAS(65 $TAB);
#CALL_PAS(30 ' --- end --- ');
##

