Sorting algorithms/Insertion sort: Difference between revisions

m
→‎{{header|REXX}}: added/changed comments and whitespace, added a title separator, extended the fence (width).
m (→‎{{header|REXX}}: added/changed comments and whitespace, added a title separator, extended the fence (width).)
Line 2,142:
=={{header|REXX}}==
<lang rexx>/*REXX program sorts a stemmed array (has characters) using the insertion sort algorithm*/
call gen /*generate the array's (data) elements. */
call show 'before sort' /*display the before array elements. */
say copies('▒', 8085) /*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: @.=; @.1 = "---Monday's Child Is Fair of Face (by Mother Goose)---"
@.2 = "Monday's child is fair of face;======================================================="
@.3 = "TuesdayMonday's child is fullfair of graceface;"
@.4 = "WednesdayTuesday's child is full of woegrace;"
@.5 = "ThursdayWednesday's child hasis farfull toof gowoe;"
@.6 = "FridayThursday's child ishas lovingfar andto givinggo;"
@.7 = "SaturdayFriday's child works hardis forloving aand livinggiving;"
@.8 = "But theSaturday's child that is bornworks onhard thefor Sabbatha dayliving;"
@.9 = "IsBut the child that blitheis andborn bonny,on goodthe andSabbath gay.day"
@.10 = "Is blithe and bonny, good and gay."
do #=1 while @.#\==''; end; #=#-1 /*determine how many entries in @ array*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
insertionSort: procedure expose @.; parse arg #
do i=2 to #; $=@.i; do j=i-1 by -1 while j\==0 & @.j>$
do j=i-1 by -_=j+1; while j\@._==0 & @.j>$
_=j+1; end @._=@./*j*/
_=j+1; end /*j*/@._=$
_=j+1; end @._=$/*i*/
end /*i*/return
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do j=1 for #; say ' element' right(j,length(#)) arg(1)": " @.j; end; return</lang>
'''output''' &nbsp; when using the internal data:
<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: TuesdayMonday's child is fullfair of graceface;
element 4 before sort: WednesdayTuesday's child is full of woegrace;
element 5 before sort: ThursdayWednesday's child hasis farfull toof gowoe;
element 6 before sort: FridayThursday's child ishas lovingfar andto givinggo;
element 7 before sort: SaturdayFriday's child works hardis forloving aand livinggiving;
element 8 before sort: But theSaturday's child that is bornworks onhard thefor Sabbatha dayliving;
element 9 before sort: IsBut the child that blitheis andborn bonny,on goodthe andSabbath gay.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 1 after sort: But---Monday's theChild childIs thatFair isof bornFace on the(by SabbathMother dayGoose)---
element 3 2 after sort: Friday's child is loving and giving;=======================================================
element 4 3 after sort: IsBut the child that blitheis andborn bonny,on goodthe andSabbath gay.day
element 5 4 after sort: MondayFriday's child is fairloving ofand facegiving;
element 6 5 after sort: Saturday'sIs childblithe worksand hardbonny, forgood aand living;gay.
element 7 6 after sort: ThursdayMonday's child hasis farfair toof goface;
element 8 7 after sort: TuesdaySaturday's child isworks hard fullfor ofa graceliving;
element 9 8 after sort: WednesdayThursday's child ishas fullfar ofto woego;
element 1 9 after sort: ---MondayTuesday's Childchild Isis Fairfull of Face (by Mother Goose)---grace;
element 10 after sort: Wednesday's child is full of woe;
</pre>