Sorting algorithms/Bubble sort: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: simplified bSort code.) |
m (→{{header|REXX}}: aligned comments, optimized bSort subroutine.) |
||
Line 3,858: | Line 3,858: | ||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
bSort: procedure expose @.; parse arg n /*N: is the number of @ array elements.*/ |
bSort: procedure expose @.; parse arg n /*N: is the number of @ array elements.*/ |
||
do m=n-1 |
do m=n-1 by -1 until ok; ok=1 /*keep sorting the @ array until done.*/ |
||
do j=1 for m; k=j+1; if @.j<=@.k then iterate /*elements in order? */ |
do j=1 for m; k=j+1; if @.j<=@.k then iterate /*elements in order? */ |
||
_=@.j; @.j=@.k; @.k=_; ok=0 /*swap two elements; flag as not done.*/ |
_=@.j; @.j=@.k; @.k=_; ok=0 /*swap two elements; flag as not done.*/ |
||
end /*j*/ |
end /*j*/ |
||
end /*m*/; return |
end /*m*/; return |
||
Line 4,012: | Line 4,012: | ||
bSort: procedure expose @.; parse arg # /*N: is the number of @ array elements.*/ |
bSort: procedure expose @.; parse arg # /*N: is the number of @ array elements.*/ |
||
call disp /*show a snapshot of the unsorted array*/ |
call disp /*show a snapshot of the unsorted array*/ |
||
do m=#-1 by -1 until ok; ok=1 |
do m=#-1 by -1 until ok; ok=1 /*keep sorting the @ array until done.*/ |
||
do j=1 for m; k=j+1 |
do j=1 for m; k=j+1 |
||
if @.j>@.k then do; parse value @.j @.k 0 with @.k @.j ok |
if @.j>@.k then do; parse value @.j @.k 0 with @.k @.j ok |