Sorting algorithms/Bubble sort: Difference between revisions

m
→‎{{header|REXX}}: added/changed comments and whitespace, changed font size for the output.
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed font size for the output.)
Line 3,389:
show: do j=1 for #; say ' element' right(j,length(#)) arg(1)":" @.j; end; return</lang>
{{out|output|text=&nbsp; when using the internal array list:}}
<br>(Shown at &nbsp; '''<sup>35</sup>/<sub>46</sub>''' &nbsp; size.)
<pre style="font-size:7584%">
element 1 before sort: ---letters of the Hebrew alphabet---
element 2 before sort: ====================================
Line 3,445:
This REXX version displays a random array of numbers &nbsp; (amount is specifiable from the command line) &nbsp; in a horizontal list.
 
Programming note: &nbsp; checksa werecheck was made to not exceed REXX's upper range limit of the &nbsp; '''random''' &nbsp; BIF.
<lang rexx>/*REXX program sorts an array (of any kind of numbers) using the bubble─sort algorithm.*/
parse arg N .; if N=='' | N=="," then N=30 /*obtain optional size of array from CL*/
call gen N /*generate the array elements (items). */
call show 'before sort:' /*show the before array elements. */
call bSort N /*invoke the bubble sort with N items.*/
call show ' after sort:' /*show the after array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
bSort: procedure expose @.; parse arg n; m=n-1 /*N: is the number of @ array elements.*/
do m=m for m by -1 until ok; ok=1 /*keep sorting the @ array until done.*/
do j=1 for m; k=j+1; if @.j>@.k then parse value @.j @.k 0 with @.k @.j ok
end /*j*/ /* [↑] swap 2 elements &, flag as ¬ done.*/
end /*m*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen: w=length(N)1; do j=1 for N; @.j=random(0, min(N+N,1000001e5)); endw=max(w,length(@.j)); end; return
show: $=arg(1); parse arg $; do jk=1 for N; $=$ right(@.jk, w); end; say $; return</lang>
{{out|output|text=&nbsp; when using a internally generated random array of thirty integers &nbsp; (which are right-justified for alignment in the display):}}
<pre>
before sort: 20 57 149 2920 31 051 37 1 14 288 25 220 1238 3042 33 341 17 285 1723 34 8 9 22 760 10 2415 2760 1954 36 213 1625 1324 2959 22 3 435 1310
after sort: 0 1 1 2 3 45 78 10 810 13 915 1020 1220 1323 1324 1425 1631 1733 1734 1935 2036 2237 2238 2241 2442 2549 2751 2854 2857 2959 2960 3060
</pre>