Sorting algorithms/Insertion sort: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added/changed comments and whitespace, added a title separator, extended the fence (width).)
Line 2,142: Line 2,142:
=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program sorts a stemmed array (has characters) using the insertion sort algorithm*/
<lang rexx>/*REXX program sorts a stemmed array (has characters) using the insertion sort algorithm*/
call gen /*generate the array's elements. */
call gen /*generate the array's (data) elements.*/
call show 'before sort' /*display the before array elements. */
call show 'before sort' /*display the before array elements. */
say copies('▒', 80) /*display a separator line (a fence). */
say copies('▒', 85) /*display a separator line (a fence). */
call insertionSort # /*invoke the insertion sort. */
call insertionSort # /*invoke the insertion sort. */
call show ' after sort' /*display the after array elements. */
call show ' after sort' /*display the after array elements. */
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen: @.=; @.1 = "---Monday's Child Is Fair of Face (by Mother Goose)---"
gen: @.=; @.1 = "---Monday's Child Is Fair of Face (by Mother Goose)---"
@.2 = "Monday's child is fair of face;"
@.2 = "======================================================="
@.3 = "Tuesday's child is full of grace;"
@.3 = "Monday's child is fair of face;"
@.4 = "Wednesday's child is full of woe;"
@.4 = "Tuesday's child is full of grace;"
@.5 = "Thursday's child has far to go;"
@.5 = "Wednesday's child is full of woe;"
@.6 = "Friday's child is loving and giving;"
@.6 = "Thursday's child has far to go;"
@.7 = "Saturday's child works hard for a living;"
@.7 = "Friday's child is loving and giving;"
@.8 = "But the child that is born on the Sabbath day"
@.8 = "Saturday's child works hard for a living;"
@.9 = "Is blithe and bonny, good and gay."
@.9 = "But the child that is born on the Sabbath day"
@.10 = "Is blithe and bonny, good and gay."
do #=1 while @.#\==''; end; #=#-1 /*determine how many entries in @ array*/
do #=1 while @.#\==''; end; #=#-1 /*determine how many entries in @ array*/
return
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
insertionSort: procedure expose @.; parse arg #
insertionSort: procedure expose @.; parse arg #
do i=2 to #; $=@.i
do i=2 to #; $=@.i; do j=i-1 by -1 while j\==0 & @.j>$
do j=i-1 by -1 while j\==0 & @.j>$
_=j+1; @._=@.j
_=j+1; @._=@.j
end /*j*/
end /*j*/
_=j+1; @._=$
_=j+1; @._=$
end /*i*/
end /*i*/
return
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do j=1 for #; say ' element' right(j,length(#)) arg(1)": " @.j; end; return</lang>
show: do j=1 for #; say ' element' right(j,length(#)) arg(1)": " @.j; end; return</lang>
'''output''' &nbsp; when using the internal data:
'''output''' &nbsp; when using the internal data:
<pre>
<pre>
element 1 before sort: ---Monday's Child Is Fair of Face (by Mother Goose)---
element 1 before sort: ---Monday's Child Is Fair of Face (by Mother Goose)---
element 2 before sort: Monday's child is fair of face;
element 2 before sort: =======================================================
element 3 before sort: Tuesday's child is full of grace;
element 3 before sort: Monday's child is fair of face;
element 4 before sort: Wednesday's child is full of woe;
element 4 before sort: Tuesday's child is full of grace;
element 5 before sort: Thursday's child has far to go;
element 5 before sort: Wednesday's child is full of woe;
element 6 before sort: Friday's child is loving and giving;
element 6 before sort: Thursday's child has far to go;
element 7 before sort: Saturday's child works hard for a living;
element 7 before sort: Friday's child is loving and giving;
element 8 before sort: But the child that is born on the Sabbath day
element 8 before sort: Saturday's child works hard for a living;
element 9 before sort: Is blithe and bonny, good and gay.
element 9 before sort: But the child that is born on the Sabbath day
element 10 before sort: Is blithe and bonny, good and gay.
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
element 1 after sort: ---Monday's Child Is Fair of Face (by Mother Goose)---
element 2 after sort: But the child that is born on the Sabbath day
element 1 after sort: ---Monday's Child Is Fair of Face (by Mother Goose)---
element 3 after sort: Friday's child is loving and giving;
element 2 after sort: =======================================================
element 4 after sort: Is blithe and bonny, good and gay.
element 3 after sort: But the child that is born on the Sabbath day
element 5 after sort: Monday's child is fair of face;
element 4 after sort: Friday's child is loving and giving;
element 6 after sort: Saturday's child works hard for a living;
element 5 after sort: Is blithe and bonny, good and gay.
element 7 after sort: Thursday's child has far to go;
element 6 after sort: Monday's child is fair of face;
element 8 after sort: Tuesday's child is full of grace;
element 7 after sort: Saturday's child works hard for a living;
element 9 after sort: Wednesday's child is full of woe;
element 8 after sort: Thursday's child has far to go;
element 9 after sort: Tuesday's child is full of grace;
element 10 after sort: Wednesday's child is full of woe;
</pre>
</pre>