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. */
@.=; @.1 = '---The seven hills of Rome:---'
call init /*assign some values to an array: @. */
@.2 = '=============================='; @.6 = 'Virminal'
@.3 = 'Caelian' ; @.7 = 'Esquiline'
@.4 = 'Palatine' ; @.8 = 'Quirinal'
@.5 = 'Capitoline' ; @.9 = 'Aventine'
do #=1 until @.#==''; end /*find the number of items in the array*/
# = # - 1 /*adjust # (because of DO index). */
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 a;; done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
init: @.=; @.1 = '---The seven hills of Rome:---'
@.2 = '=============================='; @.6 = 'Virminal'
@.3 = 'Caelian' ; @.7 = 'Esquiline'
@.4 = 'Palatine' ; @.8 = 'Quirinal'
@.5 = 'Capitoline' ; @.9 = 'Aventine'
do #=1 until @.#==''; end /*find the number of items in the array*/
#= #-1; return /*adjust # (because of DO index). */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
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 /*this item is out─of─order, swap later*/
_= @.k; p= k /*this item is out─of─order, swap later*/
end /*k*/
end /*k*/
if p==j then iterate /*if the same, the order of items is OK*/
if p==j then iterate /*if the same, the order of items is OK*/
_= @.j; @.j= @.p; @.p= _ /*swap 2 items that're out─of─sequence.*/
_= @.j; @.j= @.p; @.p= _ /*swap 2 items that're out─of─sequence.*/
end /*j*/
end /*j*/
return
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/