Sorting algorithms/Bubble sort: Difference between revisions

Content added Content deleted
(Updated with a simpler example of Julia implementation)
m (→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.)
Line 3,320: Line 3,320:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program sorts an array (of any items) using the bubble-sort algorithm.*/
<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 gen /*generate the array elements (items).*/
call show 'before sort' /*show the before array elements. */
call show 'before sort' /*show the before array elements. */
say copies('─',79) /*show a separator line (before/after).*/
say copies('─', 79) /*show a separator line (before/after).*/
call bubbleSort # /*invoke the bubble sort with # items.*/
call bubbleSort # /*invoke the bubble sort with # items.*/
call show ' after sort' /*show the after array elements. */
call show ' after sort' /*show the after array elements. */
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
bubbleSort: procedure expose @.; parse arg n /*N: number of array elements.*/
bubbleSort: procedure expose @.; parse arg n; m=n-1 /*N: number of array elements. */
m=n-1 /*use this as a handy variable for sort*/
do until ok; ok=1 /*keep sorting array until done.*/
do until done; done=1 /*keep sorting the array until done. */
do j=1 for m; k=j+1; if @.j<=@.k then iterate /*Not out-of-order?*/
do j=1 for m; k=j+1 /*search for an element out─of─order. */
_=@.j; @.j=@.k; @.k=_; ok=0 /*swap 2 elements; flag as ¬done*/
if @.j>@.k then do; _=@.j /*Out of order? Then swap two elements*/
end /*j*/
@.j=@.k /*swap current element with the next···*/
end /*until ok*/
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*/
@.3 = 'aleph [alef]' ; @.15= "mem"
end /*until ··· */
@.4 = 'beth [bet]' ; @.16= "nun"
return
@.5 = 'gimel' ; @.17= "samekh"
/*────────────────────────────────────────────────────────────────────────────*/
gen: @. = /*assign a default value to all of @. */
@.6 = 'daleth [dalet]' ; @.18= "ayin"
@.1 = '---letters of the Hebrew alphabet---' ; @.13 = 'kaph [kaf]'
@.7 = 'he' ; @.19= "pe"
@.8 = 'waw [vav]' ; @.20= "sadhe [tsadi]"
@.2 = '====================================' ; @.14 = 'lamed'
@.3 = 'aleph [alef]' ; @.15 = 'mem'
@.9 = 'zayin' ; @.21= "qoph [qof]"
@.4 = 'beth [bet]' ; @.16 = 'nun'
@.10= 'heth [het]' ; @.22= "resh"
@.5 = 'gimel' ; @.17 = 'samekh'
@.11= 'teth [tet]' ; @.23= "shin"
@.6 = 'daleth [dalet]' ; @.18 = 'ayin'
@.12= 'yod' ; @.24= "taw [tav]"
@.7 = 'he' ; @.19 = 'pe'
do #=1 while @.#\==''; end; #=#-1 /*determine #elements in list; adjust #*/
@.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
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
show: do j=1 for #; say 'element' right(j,w) arg(1)":" @.j; end; return</lang>
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:
'''output''' &nbsp; when using the internal array list:
<pre style="height:30ex">
<pre style="height:30ex">