Sorting algorithms/Comb sort: Difference between revisions

Content added Content deleted
(→‎{{header|REXX}}: added/changed whitespace and comments.)
Line 1,585: Line 1,585:


=={{header|REXX}}==
=={{header|REXX}}==
Programming note: &nbsp; the REXX statement (line 11) &nbsp; &nbsp; <big> '''s=trunc(s*.8)''' </big> &nbsp; &nbsp; could've been expressed as: &nbsp; &nbsp; <big> '''s=s*.8%1''' </big> &nbsp; &nbsp; (which is slower), but is more cryptic.
Programming note: &nbsp; the REXX statement (line 12) &nbsp; &nbsp; <big> '''s=trunc(s*.8)''' </big> &nbsp; &nbsp; could've been expressed as: &nbsp; &nbsp; <big> '''s=s*.8%1''' </big> &nbsp; &nbsp; (which is faster), but is more cryptic.
<lang rexx>/*REXX program sorts a stemmed array using the comb-sort algorithm. */
<lang rexx>/*REXX program sorts a stemmed array using the comb sort algorithm. */
call gen@; w=length(#) /*generate the array elements. */
call gen; w=length(#) /*generate the @ array elements. */
call show@ 'before sort' /*show the before array elements.*/
call show 'before sort' /*display the before array elements. */
call combSort # /*invoke the comb sort. */
say copies('▒',60) /*display a separator line (a fence). */
call show@ ' after sort' /*show the after array elements.*/
call combSort # /*invoke the comb sort. */
exit /*stick a fork in it, we're done.*/
call show ' after sort' /*display the after array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────COMBSORT subroutine─────────────────*/
/*──────────────────────────────────COMBSORT subroutine───────────────────────*/
combSort: procedure expose @.; parse arg N /*N: is number of elements.*/
combSort: procedure expose @.; parse arg N /*N: is number of @ elements. */
s=N-1 /*S: is the spread between COMBs.*/
do until s<=1 & done; done=1 /*assume sort is done (so far). */
s=N-1 /*S: is the spread between COMBs.*/
s=trunc(s*.8) /* ÷ is slow, * is better.*/
do until s<=1 & done; done=1 /*assume sort is done (so far). */
s=trunc(s*.8) /* ÷ is slow, * is better.*/
do j=1 until js>=N; js=j+s
do j=1 until js>=N; js=j+s
if @.j>@.js then do; _=@.j; @.j=@.js; @.js=_; done=0; end
if @.j>@.js then do; _=@.j; @.j=@.js; @.js=_; done=0; end
Line 1,602: Line 1,603:
end /*until*/
end /*until*/
return
return
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
/*──────────────────────────────────GEN subroutine────────────────────────────*/
gen@: @. = ; @.12 = 'dodecagon 12'
gen: @. = ; @.12 = 'dodecagon 12'
@.1 = '----polygon--- sides' ; @.13 = 'tridecagon 13'
@.1 = '----polygon--- sides' ; @.13 = 'tridecagon 13'
@.2 = '============== =======' ; @.14 = 'tetradecagon 14'
@.2 = '============== =======' ; @.14 = 'tetradecagon 14'
@.3 = 'triangle 3' ; @.15 = 'pentadecagon 15'
@.3 = 'triangle 3' ; @.15 = 'pentadecagon 15'
@.4 = 'quadrilateral 4' ; @.16 = 'hexadecagon 16'
@.4 = 'quadrilateral 4' ; @.16 = 'hexadecagon 16'
@.5 = 'pentagon 5' ; @.17 = 'heptadecagon 17'
@.5 = 'pentagon 5' ; @.17 = 'heptadecagon 17'
@.6 = 'hexagon 6' ; @.18 = 'octadecagon 18'
@.6 = 'hexagon 6' ; @.18 = 'octadecagon 18'
@.7 = 'heptagon 7' ; @.19 = 'enneadecagon 19'
@.7 = 'heptagon 7' ; @.19 = 'enneadecagon 19'
@.8 = 'octagon 8' ; @.20 = 'icosagon 20'
@.8 = 'octagon 8' ; @.20 = 'icosagon 20'
@.9 = 'nonagon 9' ; @.21 = 'hectogon 100'
@.9 = 'nonagon 9' ; @.21 = 'hectogon 100'
@.10 = 'decagon 10' ; @.22 = 'chiliagon 1000'
@.10 = 'decagon 10' ; @.22 = 'chiliagon 1000'
@.11 = 'hendecagon 11' ; @.23 = 'myriagon 10000'
@.11 = 'hendecagon 11' ; @.23 = 'myriagon 10000'
do #=1 while @.#\==''; end; #=#-1 /*find how many entries in array.*/
do #=1 while @.#\==''; end; #=#-1 /*determine how many entries in @ array*/
return
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
/*──────────────────────────────────SHOW subroutine───────────────────────────*/
show@: say copies('▒',60); do j=1 for # /*display array elements.*/
show: do j=1 for #; say ' element' right(j,w) arg(1)":" @.j; end; return</lang>
say ' element' right(j,w) arg(1)":" @.j
end /*j*/
return</lang>
Data note: &nbsp; A &nbsp; ''hendecagon'' &nbsp; (also known as an &nbsp; ''undecagon''
Data note: &nbsp; A &nbsp; ''hendecagon'' &nbsp; (also known as an &nbsp; ''undecagon''
&nbsp; or &nbsp; ''unidecagon'') &nbsp; is from the Greek word &nbsp;
&nbsp; or &nbsp; ''unidecagon'') &nbsp; it is from the Greek word &nbsp;
''hendeka'' &nbsp; [eleven] &nbsp; and &nbsp; ''gon─'' &nbsp; [corner]. <br>
''hendeka'' &nbsp; [eleven] &nbsp; and &nbsp; ''gon─'' &nbsp; [corner]. <br>


'''output'''
'''output'''
<pre style="height:80ex">
<pre style="height:80ex">
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
element 1 before sort: ----polygon--- sides
element 1 before sort: ----polygon--- sides
element 2 before sort: ============== =======
element 2 before sort: ============== =======