Sorting algorithms/Bubble sort: Difference between revisions

→‎{{header|REXX}}: added a REXX version with snapshots of the sort's progress.
m (→‎version 1, random integers, horizontal list: added wording to the REXX section header.)
(→‎{{header|REXX}}: added a REXX version with snapshots of the sort's progress.)
Line 3,918:
<pre>vorher : 9 17 16 19 5 7 3 20 16 0
nachher: 0 3 5 7 9 16 16 17 19 20</pre>
 
===version 3, random integers, horizontal list, with interim plots===
This REXX program is a modified version of the first REXX program, with produces a snapshot of the plot in progress.
 
Note that the command to clear the terminal screen is hard-coded as: &nbsp; '''CLS'''
<lang rexx>/*REXX program sorts an array (of any kind of numbers) using the bubble─sort algorithm.*/
parse arg N seed . /*obtain optional size of array from CL*/
if N=='' | N=="," then N=30 /*Not specified? Then use the default.*/
if datatype(seed, 'W') then call random ,,seed /*Not specified? Then use the default.*/
call gen N /*generate the array elements (items). */
call show 'before sort:' /*show the before array elements. */
$$= $ /*keep "before" copy for after the sort*/
call bSort N /*invoke the bubble sort with N items.*/
say $$
call show ' after sort:' /*show the after array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
bSort: procedure expose @.; parse arg #; m=#-1 /*N: is the number of @ array elements.*/
call disp /*show a snapshot of the unsorted array*/
do m=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 do; parse value @.j @.k 0 with @.k @.j ok
end
end /*j*/ /* [↑] swap 2 elements, flag as ¬done.*/
call disp /*show snapshot of partically sorted @.*/
end /*m*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen: do j=1 for N; @.j= j; end
do k=1 for N; g= random(1,N); parse value @.k @.g with @.g @.k; end; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: parse arg $; do k=1 for N; $=$ right(@.k, length(N)); end; say $; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
disp: 'CLS'; $.= /*"CLS" is the command to clear screen.*/
do e=1 for #; $.e= '│'overlay("☼", $.e, @.e); end /*e*/
do s=# for # by -1; say $.s; end /*s*/
say "└"copies('─', #) /*display the horizontal axis at bottom*/
return</lang>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│☼
│ ☼
│ ☼
│ ☼ at 0% sorted
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
└────────────────────────
</pre>
<pre>
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼ at about 25% sorted
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│☼
│ ☼
└──────────────────────────────
</pre>
<pre>
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼ at about 50% sorted
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│☼
└──────────────────────────────
</pre>
<pre>
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼ at 100% sorted
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│ ☼
│☼
└──────────────────────────────
 
before sort: 11 3 15 4 12 14 7 20 22 5 8 19 24 13 6 1 16 23 17 2 10 9 21 18
after sort: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
</pre>
 
=={{header|Ring}}==