kill(all)$

maxima :program : while with 'example bubblesort',version 1.0
peter.vlasschaert@gmail.com,31/05/2019

n:2;

syntax : while function.
===================

flag:0;

while flag=0 do(
if n=2 then
    ( flag :1, n:n+5),
    n:n+1,flag:1);

n;

testwhile(m):= block([flag:0],
  while flag=0 do(
if m=2 then
    ( flag :1, m:m+5),
    m:m+1,flag:1),return(m));
    

?  testwhile(2)
============
result :  m:m+5, m:m+1

testwhile(2);

?  testwhile(for m ≠ 2)
==================
result : m:m+1

testwhile(4);

given list data : 
=============

before : sort
===========

a:[7,2,3,1,4,5];

kill(flag);

m1:length(a);

flag;

flag:0$
while (flag=0) do
(flag :1,
for i:1 thru m1-1
    do(
            if a[i] > a[i+1] then
            (   temp : a[i],
               a[i]:a[i+1],
               a[i+1]:temp,flag:0)     
        )
);
    

after : sort
=========

a;

kill(flag)!;

flag;

kill(a);

b:[4,2,8,3,1];

function : bubblesort_while(mx)

flags;

bubblesortwhile(qq):= block( [],
count:0,
flags:0,
mx:length(qq),
while ( flags=0) do
(flags :1,
for i:1 thru mx-1
    do(
        count :count +1,
            if qq[i] > qq[i+1] then
            (   temp : qq[i],
               qq[i]:qq[i+1],
               qq[i+1]:temp,flags:0)     
        )
    
 ),return(qq)
                                                 );

return

bubblesortwhile(b);

number changes:

count;

kill(count);

a) function : swaplist.
==================

swaplist(qq,i):=block([],
               temp : qq[i],
               qq[i]:qq[i+1],
               qq[i+1]:temp );
    

b) function : bubblesortnwhile

bubblesortnwhile(qq):= block( [],
count:0,
flags:0,
mx:length(qq),
while ( flags=0) do
(flags :1,
for i:1 thru mx-1
    do(
        count :count +1,
            if qq[i] > qq[i+1] then
            (swaplist(qq,i),flags:0)     
        )
    
 ),return([qq,count])                              );

example : with two numbers are the same.
====================================

s1:bubblesortnwhile([4,5,7,9,1,5,3]);

before sort :[4,5,7,9,1,5,3]
after     sort :[1,3,4,5,5,7,9]

count;

info : from output , with use 'command', return.
=======================================

s2:s1[1];
s2:s1[2];