Sorting algorithms/Insertion sort: Difference between revisions
Content added Content deleted
(Added PicoLisp) |
m (→{{header|R}}) |
||
Line 962: | Line 962: | ||
} |
} |
||
insertionsort(c(4, 65, 2, -31, 0, 99, 83, 782, 1)) # -31 0 1 2 4 65 83 99 782</lang> |
insertionsort(c(4, 65, 2, -31, 0, 99, 83, 782, 1)) # -31 0 1 2 4 65 83 99 782</lang> |
||
=={{header|REXX}}== |
|||
<lang rexx> |
|||
/*REXX program sorts an array using the insertion-sort method. */ |
|||
call gen@ /*generate array elements. */ |
|||
call show@ 'before sort' /*show before array elements*/ |
|||
call insertionSort highItem /*invoke the insertion sort.*/ |
|||
call show@ ' after sort' /*show after array elements*/ |
|||
exit |
|||
/*─────────────────────────────────────INSERTIONSORT subroutine────*/ |
|||
insertionSort: procedure expose @.; parse arg highItem |
|||
do i=2 to highItem |
|||
value=@.i |
|||
do j=i-1 by -1 while j\==0 & @.j>value |
|||
jp1=j+1 |
|||
@.jp1=@.j |
|||
end |
|||
jp1=j+1 |
|||
@.jp1=value |
|||
end |
|||
return |
|||
/*─────────────────────────────────────GEN@ subroutine─────────────*/ |
|||
gen@: @.='' /*assign default value. */ |
|||
@.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 highItem=1 while @.highItem\=='' /*find how many entries. */ |
|||
end |
|||
highItem=highItem-1 /*adjust highItem slightly. */ |
|||
return |
|||
/*─────────────────────────────────────SHOW@ subroutine────────────*/ |
|||
show@: widthH=length(highItem) /*maximum widht of any line.*/ |
|||
do j=1 for highItem |
|||
say 'element' right(j,widthH) arg(1)':' @.j |
|||
end |
|||
say copies('─',80) /*show a seperator line. */ |
|||
return |
|||
</lang> |
|||
Output: |
|||
<pre style="height:30ex;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> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |