Sorting algorithms/Bubble sort: Difference between revisions

m
→‎{{header|REXX}}: added/changed whitespace and comments, simplified the bubble sort algorithm.
No edit summary
m (→‎{{header|REXX}}: added/changed whitespace and comments, simplified the bubble sort algorithm.)
Line 3,323:
 
=={{header|REXX}}==
<lang rexx>/*REXX program sorts an array (of any items) using the bubble-sort algorithm. */
call gen@ /*generate the array elements. (items).*/
call show@ 'before sort' /*show the before array elements. */
call bubbleSort # say copies('─',79) /*invokeshow thea bubbleseparator sortline (before/after). */
call show@bubbleSort # ' after sort' /*showinvoke the bubble sort afterwith array# elementsitems.*/
exitcall show ' after sort' /*show the after array elements. /*stick a fork in it, we're done.*/
exit done=0 /*indicatestick a fork in it, we'sre notall done,. whereas*/
/*──────────────────────────────────BUBBLESORT subroutine───────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
bubbleSort: procedure expose @.; parse arg n /*N: number of itemsarray elements.*/
/*diminish # items each time. */
m=n-1 do until done /*sort until it's done. /*use this as a handy variable for sort*/
done=1 do until done; done=1 /*assumekeep it'ssorting donethe array until (1done. true). */
do j=1 for n-1 m; k=j+1 /*sortsearch Mfor itemsan thiselement time aroundout─of─order. */
if @.j>@.k=j+1 then do; _=@.j /*point to the next item. Out of order? Then swap two elements*/
if @.j>@.k then do /*is it out of order? @.j=@.k /*swap current element with the next···*/
_=@.j @.k=_ /*assign to a temp variable. ··· and the next with _ */
@.j=@.k done=0 /*swapindicate currentthat itemthe withsorting nextisn't ···done,*/
@.k=_end /* ··· and the next with _ (1≡true, 0≡false). */
done=0 /*indicate it's not done, whereas*/
end /* [↑] 1≡true 0≡false */
end /*j*/
end end /*until done··· */
return
/*────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
gen@: @.= = /*assign a default value to all of @. */
@.1 = '---letters of the Hebrew alphabet---' ; @.13 = 'kaph [kaf]'
@.2 = '====================================' ; @.14 = 'lamed'
@.3 = 'aleph [alef]' ; @.15 = 'mem'
@.4 = 'beth [bet]' ; @.16 = 'nun'
@.5 = 'gimel' ; @.17 = 'samekh'
@.6 = 'daleth [dalet]' ; @.18 = 'ayin'
@.7 = 'he' ; @.19 = 'pe'
@.8 = 'waw [vav]' ; @.20 = 'sadhe [tsadi]'
@.9 = 'zayin' ; @.21 = 'qoph [qof]'
@.10 = 'heth [het]' ; @.22 = 'resh'
@.11 = 'teth [tet]' ; @.23 = 'shin'
@.12 = 'yod' ; @.24 = 'taw [tav]'
do #=1 while @.#\==''; end; #=#-1 /*find how many elements in list.*/
 
w=length(#) do #=1 while @.# \=='' /*findthe howmaximum manywidth entriesof inany listindex. */
end /*#*/return
/*────────────────────────────────────────────────────────────────────────────*/
#=#-1 /*adjust because of DO increment.*/
show: do j=1 for #; say 'element' right(j,widthHw) arg(1)'":' " @.j; end; return</lang>
return
'''output''' &nbsp; when using the internal array list:
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@: widthH=length(#) /*maximum width of any line. */
do j=1 for #
say 'element' right(j,widthH) arg(1)':' @.j
end /*j*/
say copies('─',80) /*show a separator line. */
return</lang>
{{out}}
<pre style="height:30ex">
element 1 before sort: ---letters of the Hebrew alphabet---
Line 3,397 ⟶ 3,388:
element 23 before sort: shin
element 24 before sort: taw [tav]
───────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────────
element 1 after sort: ---letters of the Hebrew alphabet---
element 2 after sort: ====================================
Line 3,422 ⟶ 3,413:
element 23 after sort: yod
element 24 after sort: zayin
────────────────────────────────────────────────────────────────────────────────
</pre>