Sorting algorithms/Insertion sort: Difference between revisions
Content added Content deleted
Capra Hircus (talk | contribs) m (adding maxima) |
(→{{header|REXX}}: changed DO loop indentations, changed comments, added comments, changed fence, re-order subroutines in alphbetical order. -- ~~~~) |
||
Line 1,552: | Line 1,552: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX program sorts |
<lang rexx>/*REXX program sorts a stemmed array using the insertion-sort method.*/ |
||
call gen@ /*generate the array's elements. */ |
call gen@ /*generate the array's elements. */ |
||
call show@ 'before sort' /*show the before array elements.*/ |
call show@ 'before sort' /*show the before array elements.*/ |
||
call insertionSort highItem /*invoke the insertion sort. */ |
call insertionSort highItem /*invoke the insertion sort. */ |
||
call show@ ' after sort' /*show the after array elements.*/ |
call show@ ' after sort' /*show the after array elements.*/ |
||
⚫ | |||
exit |
|||
/*──────────────────────────────────GEN@ subroutine─────────────────────*/ |
|||
/*─────────────────────────────────────INSERTIONSORT subroutine─────────*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
/*─────────────────────────────────────GEN@ subroutine──────────────────*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
do highItem=1 while @.highItem\=='' /*find how many entries in array.*/ |
do highItem=1 while @.highItem\=='' /*find how many entries in array.*/ |
||
end /*short and sweet DO loop, eh? */ |
end /*short and sweet DO loop, eh? */ |
||
highItem=highItem-1 /*because of DO, adjust highItem.*/ |
highItem=highItem-1 /*because of DO, adjust highItem.*/ |
||
return |
return |
||
/*──────────────────────────────────INSERTIONSORT subroutine────────────*/ |
|||
/*─────────────────────────────────────SHOW@ subroutine─────────────────*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end /*j*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
/*──────────────────────────────────SHOW@ subroutine────────────────────*/ |
|||
show@: widthH=length(highItem) /*the maximum width of any line. */ |
show@: widthH=length(highItem) /*the maximum width of any line. */ |
||
do j=1 for highItem |
do j=1 for highItem |
||
say 'element' right(j,widthH) arg(1)':' @.j |
say 'element' right(j,widthH) arg(1)': ' @.j |
||
end |
end /*j*/ |
||
say copies('─',79) /*show a |
say copies('─',79) /*show a seperator line that fits*/ |
||
return</lang> |
return</lang> |
||
'''output''' |
'''output''' |
||
<pre style="height: |
<pre style="height:40ex;overflow:scroll"> |
||
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> |
</pre> |
||