Sorting algorithms/Selection sort: Difference between revisions
Content added Content deleted
m (→{{header|Ruby}}: closing </lang> tag) |
m (→{{header|REXX}}: added/changed comments and whitespace, added a subroutine to create the array.) |
||
Line 2,584: | Line 2,584: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX program sorts a stemmed array using the selection─sort algorithm. */ |
<lang rexx>/*REXX program sorts a stemmed array using the selection─sort algorithm. */ |
||
call init /*assign some values to an array: @. */ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
call show 'before sort' /*show the before array elements. */ |
call show 'before sort' /*show the before array elements. */ |
||
say copies('▒', 65) /*show a nice separator line (fence). */ |
say copies('▒', 65) /*show a nice separator line (fence). */ |
||
call selectionSort # /*invoke selection sort (and # items). */ |
call selectionSort # /*invoke selection sort (and # items). */ |
||
call show ' after sort' /*show the after array elements. */ |
call show ' after sort' /*show the after array elements. */ |
||
exit /*stick a fork in it, we're |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
init: @.=; @.1 = '---The seven hills of Rome:---' |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
selectionSort: procedure expose @.; parse arg n |
selectionSort: procedure expose @.; parse arg n |
||
do j=1 for n-1; _= @.j; p= j |
do j=1 for n-1; _= @.j; p= j |
||
do k=j+1 to n; if @.k>=_ then iterate |
do k=j+1 to n; if @.k>=_ then iterate |
||
_= @.k; p= k |
_= @.k; p= k /*this item is out─of─order, swap later*/ |
||
end /*k*/ |
end /*k*/ |
||
if p==j then iterate |
if p==j then iterate /*if the same, the order of items is OK*/ |
||
_= @.j; @.j= @.p; @.p= _ |
_= @.j; @.j= @.p; @.p= _ /*swap 2 items that're out─of─sequence.*/ |
||
end /*j*/ |
end /*j*/ |
||
return |
return |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |