Jump to content

Sorting algorithms/Insertion sort: Difference between revisions

m
→‎{{header|REXX}}: added/changed comments and whitespace, changed indentations.
m (added whitespace before the TOC (table of contents).)
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed indentations.)
Line 2,061:
 
=={{header|REXX}}==
<lang rexx>/*REXX program sorts a stemmed array (has characters) using the insertion sort algorithm. */
call gen /*generate the array's elements. */
call show 'before sort' /*display the before array elements. */
say copies('▒',79 80) /*display a separator line (a fence). */
call insertionSort # /*invoke the insertion sort. */
call show ' after sort' /*display the after array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────GEN subroutine────────────────────────────*/
gen: @.=; @.1 = "---Monday's Child Is Fair of Face (by Mother Goose)---"
@.2 = "Monday's child is fair of face;"
@.3 = "Tuesday's child is full of grace;"
@.4 = "Wednesday's child is full of woe;"
@.5 = "Thursday's child has far to go;"
@.6 = "Friday's child is loving and giving;"
@.7 = "Saturday's child works hard for a living;"
@.8 = "But the child that is born on the Sabbath day"
@.9 = "Is blithe and bonny, good and gay."
do #=1 while @.#\==''; end; #=#-1 /*determine how many entries in @ array*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────INSERTIONSORT subroutine──────────────────*/
insertionSort: procedure expose @.; parse arg #
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
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────SHOW subroutine───────────────────────────*/
show: do j=1 for #; say ' element' right(j,length(#)) arg(1)'": '" @.j; end; return</lang>
'''output''' &nbsp; when using the internal data:
{{out}}
<pre>
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 3 before sort: Tuesday's child is full of grace;
element 4 before sort: Wednesday's child is full of woe;
element 5 before sort: Thursday's child has far to go;
element 6 before sort: Friday's child is loving and giving;
element 7 before sort: Saturday's child works hard for a living;
element 8 before sort: But the child that is born on the Sabbath day
element 9 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 3 after sort: Friday's child is loving and giving;
element 4 after sort: Is blithe and bonny, good and gay.
element 5 after sort: Monday's child is fair of face;
element 6 after sort: Saturday's child works hard for a living;
element 7 after sort: Thursday's child has far to go;
element 8 after sort: Tuesday's child is full of grace;
element 9 after sort: Wednesday's child is full of woe;
───────────────────────────────────────────────────────────────────────────────
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.