Sort an integer array: Difference between revisions
Content added Content deleted
m (→sort a list: used a smaller font for the output.) |
m (→sort a list: added/changed some comments and whitespace.) |
||
Line 1,767: | Line 1,767: | ||
Bern= '1 -1 1 0 -1 0 1 0 -1 0 5 0 -691 0 7 0 -3617' /*" " Bernoulli " */ |
Bern= '1 -1 1 0 -1 0 1 0 -1 0 5 0 -691 0 7 0 -3617' /*" " Bernoulli " */ |
||
Perrin= 3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90 /*" " Perrin " */ |
Perrin= 3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90 /*" " Perrin " */ |
||
list=Bell Bern Perrin |
list= Bell Bern Perrin /*throw them all ───► a pot. */ |
||
say 'unsorted =' list /*display what's being shown.*/ |
say 'unsorted =' list /*display what's being shown.*/ |
||
#= words(list) /*nice to have # of elements.*/ |
|||
do j=1 for |
do j=1 for # /*build an array, a single */ |
||
@.j=word(list,j) |
@.j=word(list, j) /* ··· element at a time.*/ |
||
end /*j*/ |
end /*j*/ |
||
call eSort |
call eSort # /*sort the collection of #s. */ |
||
$= |
$=; do k=1 for #; $= $ @.k /*build a list from the array*/ |
||
do k=1 for size /*build a list from the array*/ |
|||
$=$ @.k /*append a number to the list*/ |
|||
end /*k*/ |
end /*k*/ |
||
say ' sorted =' space($) /*display the sorted list. */ |
say ' sorted =' space($) /*display the sorted list. */ |
||
exit /*stick a fork in it, we're all done.*/ |
exit /*stick a fork in it, we're all done.*/ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
eSort: procedure expose @.; parse arg N; h= |
eSort: procedure expose @.; parse arg N; h= N /*an eXchange sort.*/ |
||
do while h>1; h= h% |
do while h>1; h= h % 2 /*define a segment.*/ |
||
do i=1 for N-h; j= i; k= h + i /*sort top segment.*/ |
|||
do while @.k<@.j /*see if need swap.*/ |
|||
parse value @.j @.k with @.k @.j /*swap two elements*/ |
|||
if h>=j then leave; j= j - h; k= k - h /*this part sorted?*/ |
|||
end /*while @.k<@.j*/ |
|||
end /*i*/ |
|||
end |
end /*while h>1*/ |
||
return</lang> |
return</lang> |
||
{{out|output|text= when using the default internal inputs:}} |
{{out|output|text= when using the default internal inputs:}} |