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('▒', |
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: |
gen: @.=; @.1 = "---Monday's Child Is Fair of Face (by Mother Goose)---" |
||
@.2 = " |
@.2 = "=======================================================" |
||
@.3 = " |
@.3 = "Monday's child is fair of face;" |
||
@.4 = " |
@.4 = "Tuesday's child is full of grace;" |
||
@.5 = " |
@.5 = "Wednesday's child is full of woe;" |
||
@.6 = " |
@.6 = "Thursday's child has far to go;" |
||
@.7 = " |
@.7 = "Friday's child is loving and giving;" |
||
@.8 = " |
@.8 = "Saturday's child works hard for a living;" |
||
@.9 = " |
@.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 @.; |
insertionSort: procedure expose @.; parse arg # |
||
do i=2 to #; |
do i=2 to #; $=@.i; do j=i-1 by -1 while j\==0 & @.j>$ |
||
_=j+1; @._=@.j |
|||
end /*j*/ |
|||
_=j+1; @._=$ |
|||
end /*i*/ |
|||
return |
|||
return |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
show: |
show: do j=1 for #; say ' element' right(j,length(#)) arg(1)": " @.j; end; return</lang> |
||
'''output''' when using the internal data: |
'''output''' 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: |
element 2 before sort: ======================================================= |
||
element 3 before sort: |
element 3 before sort: Monday's child is fair of face; |
||
element 4 before sort: |
element 4 before sort: Tuesday's child is full of grace; |
||
element 5 before sort: |
element 5 before sort: Wednesday's child is full of woe; |
||
element 6 before sort: |
element 6 before sort: Thursday's child has far to go; |
||
element 7 before sort: |
element 7 before sort: Friday's child is loving and giving; |
||
element 8 before sort: |
element 8 before sort: Saturday's child works hard for a living; |
||
element 9 before sort: |
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 |
element 1 after sort: ---Monday's Child Is Fair of Face (by Mother Goose)--- |
||
element |
element 2 after sort: ======================================================= |
||
element |
element 3 after sort: But the child that is born on the Sabbath day |
||
element |
element 4 after sort: Friday's child is loving and giving; |
||
element |
element 5 after sort: Is blithe and bonny, good and gay. |
||
element |
element 6 after sort: Monday's child is fair of face; |
||
element |
element 7 after sort: Saturday's child works hard for a living; |
||
element |
element 8 after sort: Thursday's child has far to go; |
||
⚫ | |||
element 10 after sort: Wednesday's child is full of woe; |
|||
</pre> |
</pre> |
||