Anonymous user
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= when using the internal array list:}}
<br>(Shown at '''<sup>
<pre style="font-size:
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 (amount is specifiable from the command line) in a horizontal list.
Programming note:
<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
call gen N /*generate the array elements
call show 'before sort:'
call bSort N /*invoke the bubble sort with N items.*/
call show ' after sort:'
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
end /*j*/
end /*m*/;
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen: w=
show:
{{out|output|text= when using a internally generated random array of thirty integers (which are right-justified for alignment in the display):}}
<pre>
before sort: 20 57
after sort: 0 1
</pre>
|