Anonymous user
Sorting algorithms/Bubble sort: Difference between revisions
m
→{{header|REXX}}: added a horizontal (output) version, optimized the bubble sort subroutines.
m (→version 1: expanded the output window to show more values at-a-glance.) |
m (→{{header|REXX}}: added a horizontal (output) version, optimized the bubble sort subroutines.) |
||
Line 3,281:
=={{header|REXX}}==
===version
This REXX version displays an array (of alpha-numeric items) in a vertical list.
<lang rexx>/*REXX program sorts an array (of any kind of items) using the bubble─sort algorithm.*/
call gen /*generate the array elements (items).*/
call show 'before sort' /*show the before array elements. */
say copies('▒', 70) /*show a separator line (before/after).*/
call
call show ' after sort' /*show the after array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
end /*m*/;
return▼
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen: @.=; @.1 = '---letters of the Hebrew alphabet---' ; @.13= "kaph [kaf]"
Line 3,368:
</pre>
===version
This REXX version displays a random array of numbers (amount is specifiable from the command line) in a horizontal list.
Programming note: checks were made to not exceed REXX's range limit of the '''random''' 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); do j=1 for N; @.j=random(0, min(N,100000)); end; return
show: $=arg(1); do j=1 for N; $=$ right(@.j, w); end; say $; return</lang>
{{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 1 29 0 1 14 28 25 22 12 30 3 17 28 17 8 9 22 7 10 24 27 19 2 16 13 29 22 4 13
after sort: 0 1 1 2 3 4 7 8 9 10 12 13 13 14 16 17 17 19 20 22 22 22 24 25 27 28 28 29 29 30
</pre>
===version 2, random integers, horizontal list===
{{trans|PL/I}}
<lang rexx>Call random ,,1000
|