kill(all)$

Maxima :bubble sort with two for next loops. version 1.0
peter.vlasschaert@gmail.com,29/05/2019

info : c++ , 'see below'

p1:[1,3,2,4,7,9,5];

sort : build in Maxima.

p2:sort(p1);

how to use block
==============

myblock(n):=block([m:n],test ,if  n< 2 then return(m) else return( [m,"test"]),go(test));

myblock(3);

list : 'pp'
------------

pp:p1;

element of list : 'pp'
------------------------------

pp[2];

sort :algorithm ( = bubble sort).
==========================

c++  
===
Algorithm–Bubble Sort

int a[] = {5, 3, 2, 1, 4};
for (int i= 4; i>= 0; --i)
for (int j = 0; j < i; ++j)
if (a[j] > a[j+1])
 {inttemp = a[j];
   a[j] = a[j+1];
    a[j+1] = temp}

rem: init i =4 => 3 => 2 => 1 => 0
see : 'true basic'

true basic
=========
Algorithm–Bubble Sort

option nolet
! based crossing and uncrossing (algo)
dim a(1)   ! c++  dim a(0)
n=5           ! c++   n=4
for i=1 to n
  mat redim a(i)
  read a(i)
next i
!=>      ' data array '
data 5,3,2,1,4
print "before sorting =>"
mat print a
!****************************************
  for i= n to 0 step -1     ! step down.
     for j = i+1 to  n-1 
      if a(j)> a(j+1) then 
         call swap(a(),j)     
      end if
       next j
   next i
!****************************************
print "after sorting =>"
mat print a
end

sub swap(a(),i)
!===swap======= 
    temp  =a(i)
    a(i)  =a(i+1)
    a(i+1)=temp
!==============
end sub

maple
=========
Algorithm–Bubble Sort

BubbleSort:=proc(L::list)
  local i, j, temp, T;
  T:= Array(L);
  for i from 1 to nops(L)-1 do
    for j from 1 to nops(L)-i do
      if T[j] > T[j+1] then
        temp:=T[j];
        T[j] := T[j+1];
        T[j+1] := temp;
      end if
    end do
  end do;
  convert(T,list)
end proc:

BubbleSort([3,2,4,1,5]);

see : 

http://www.mhhe.com/math/advmath/rosen/r5/student/ch09/maple.html
by Kenneth H. Rosen, AT&T Laboratories (with very minor changes by me: array->Array).

Maxima  
=======
Algorithm–Bubble Sort

<==============================================================================>

swap : two entries
 5,4  =>  4,5  ( sort )

swap : routine
----------------------

swap(a,i) := block([],
    temp :a[i],
   a[i]  : a[i+1],
    a[i+1]:temp );

bubble sort : routine
-------------------------------

pp;

two for loops  1e)  up
                           2e)  up
--------------------------------

    m:length(pp);
    for  i: 1  thru m
    do (
     for j : i  thru  m-1
        do (
      if pp[j] >pp [j+1] then 
       ( temp :pp[j],
       pp[j]  : pp[j+1],
        pp[j+1]:temp )   
         )
        );      ;

pp;

two for loops  1e) down : step -1
                           2e)  up     : step   1
-------------------------------------------------

qq:[1,3,2,4,7,9,5];

 mm:length(qq);
    for  i: mm  thru 0 step -1
    do (
     for j : i+1  thru  mm-1 step 1
        do (
      if qq[j] >qq[j+1] then 
       ( temp :qq[j],
       qq[j]  : qq[j+1],
        qq[j+1]:temp )   
         )
        );      ;

rem : not need in the code : step 1

qq;

kill(qq);

qq;

qq:[1,3,2,4,7,9,5];

 mm:length(qq);
    for  i: mm  thru 0 step -1
    do (
     for j : i+1  thru  mm-1 step 1
        do (
      if qq[j] >qq[j+1] then 
       ( swap(qq,j) )   
         )
        );      ;

qq;

kill(qq);

qq:[1,3,2,4,7,9,5];

mybubblesort(aa):=block([],
     m1:length(aa),
    for  i: m1  thru 0 step -1
    do (
     for j : i+1  thru  m1-1 step 1
        do (        
      if aa[j] >aa[j+1] then 
          ( temp :aa[j],
            aa[j]  : aa[j+1],
            aa[j+1]:temp )    
            )        
        ),
     return(aa)
   );

qq;

mybubblesort(qq);

other example :  mybubblesort(list).
==============================

rr:[4,8,9,2,1,6,3,5];

mybubblesort(rr);

mybubblesortc(aa):=block([],
    su : 0,
     m1:length(aa),
    for  i: m1  thru 0 step -1
       do (
     for j : i+1  thru  m1-1 step 1
        do (        
      if aa[j] >aa[j+1] then     
           ( su:su+1,
            temp :aa[j],
            aa[j]   : aa[j+1],
            aa[j+1]:temp    )    
            )        
        ),
     return([aa,su])
   );

su : number of operations, "mybubblesortc".

sort examples :
=============

tt:[4,8,9,2,1,6,3,5];

mybubblesortc(tt);

ll:[2,3,1];

pa:mybubblesortc(ll);

part list : 'pa'
-------------------

pa1:pa[1];
pa2:pa[2];