Sorting algorithms/Bubble sort: Difference between revisions

m
→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.
(Updated with a simpler example of Julia implementation)
m (→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.)
Line 3,320:
 
=={{header|REXX}}==
<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('─', 79) /*show a separator line (before/after).*/
call bubbleSort # /*invoke the bubble sort with # items.*/
call show ' after sort' /*show the after array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
bubbleSort: procedure expose @.; parse arg n; m=n-1 /*N: number of array elements. */
m=n-1 do until ok; ok=1 /*use this as a handy variable for sort /*keep sorting array until done.*/
do until done; done=1 do j=1 /*keep sortingfor them; array untilk=j+1; done if @.j<=@.k then iterate /*Not out-of-order?*/
do j=1 for m _=@.j; @.j=@.k; @.k=j+1_; ok=0 /*searchswap for2 anelements; elementflag as out─of─order. ¬done*/
if @.j>@.k then do; _=@.j /*Out ofend order? Then swap two elements/*j*/
end @.j=@.k /*swap current element with theuntil next···ok*/
end /*j*/return
@.k=_ /* ··· and the next with _ */
/*──────────────────────────────────────────────────────────────────────────────────────*/
done=0 /*indicate that the sorting isn't done,*/
gen: @.=; @.1 = '---letters of the Hebrew alphabet---' ; @.13= "kaph [kaf]"
end /* (1≡true, 0≡false). */
@.2 = '====================================' ; @.14 = '"lamed'"
end /*j*/
@.103 = 'heth aleph [hetalef]' ; @.22 15= 'resh'"mem"
end /*until ··· */
@.4 = 'beth [bet]' @.k=_ /* ··· and the; next with @.16= _ */"nun"
return
@.5 = 'gimel' end /* (1≡true, ; 0≡false). @.17= */"samekh"
/*────────────────────────────────────────────────────────────────────────────*/
gen: @. = @.6 = 'daleth [dalet]' /*assign a default value to all of ; @.18= */"ayin"
@.1 = '---letters of the Hebrew alphabet---' ; @.137 = 'kaphhe' [kaf]' ; @.19= "pe"
@.8 = 'waw [vav]' ; @.20 = '"sadhe [tsadi]'"
@.2 = '====================================' ; @.14 = 'lamed'
@.3 9 = 'alephzayin' [alef]' ; @.15 21= 'mem'"qoph [qof]"
@.4 @.10= 'bethheth [bethet]' ; @.16 22= 'nun'"resh"
@.5 = 'gimel' @.11= 'teth [tet]' ; @.17 23= 'samekh'"shin"
@.6 @.12= 'dalethyod' [dalet]' ; @.18 24= 'ayin'"taw [tav]"
@.7 = 'he' do #=1 while @.#\==''; end; #=#-1 /*determine #elements in list; adjust @.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(#) /*the maximum width of any index.*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
show: w=length(#); do j=1 for #; say 'element' right(j,w) arg(1)":" @.j; end; return</lang>
'''output''' &nbsp; when using the internal array list:
<pre style="height:30ex">