Sort an integer array: Difference between revisions
Content added Content deleted
m (→sort an array: added/changed whitespace, used a sparse array, eliminated zero-valued array elements.) |
m (→sort a list: changed comments and indentations, removed STYLE from the PRE html tag, changed right arrow glyph.) |
||
Line 1,332: | Line 1,332: | ||
<br>sorted, than the list is re-constituted. |
<br>sorted, than the list is re-constituted. |
||
<lang rexx>/*REXX program sorts (using E-sort) a list of some interesting integers.*/ |
<lang rexx>/*REXX program sorts (using E-sort) a list of some interesting integers.*/ |
||
⚫ | |||
⚫ | |||
bell= 1 1 2 5 15 52 203 877 4140 21147 115975 /*some Bell numbers.*/ |
bell= 1 1 2 5 15 52 203 877 4140 21147 115975 /*some Bell numbers.*/ |
||
bern= '1 -1 1 0 -1 0 1 0 -1 0 5 0 -691 0 7 0 -3617' /*some Bernoulli num*/ |
bern= '1 -1 1 0 -1 0 1 0 -1 0 5 0 -691 0 7 0 -3617' /*some Bernoulli num*/ |
||
perrin=3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90 |
perrin= 3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90 /*some Perrin nums. */ |
||
list=bell bern perrin /*throw ' |
list=bell bern perrin /*throw 'em───►a pot*/ |
||
say ' |
say 'unsorted =' list /*an annoucement··· */ |
||
size=words(list) /*nice to have SIZE.*/ |
size=words(list) /*nice to have SIZE.*/ |
||
do j=1 for size |
do j=1 for size /*build an array, 1 */ |
||
@.j=word(list,j) /*element at a time.*/ |
|||
end /*j*/ |
end /*j*/ |
||
call esort size /*sort the stuff. */ |
call esort size /*sort the stuff. */ |
||
bList= |
bList= /*list: null so far.*/ |
||
do k= |
do k=1 for size /*build a list. */ |
||
bList=bList |
bList=strip(bList @.k) /*append it to list.*/ |
||
end /*k*/ |
end /*k*/ |
||
say ' sorted =' bList /*show & tell time. */ |
say ' sorted =' bList /*show & tell time. */ |
||
exit /*stick a fork in it, we're done.*/ |
exit /*stick a fork in it, we're done.*/ |
||
/*──────────────────────────────────ESORT subroutine────────────────────*/ |
/*──────────────────────────────────ESORT subroutine────────────────────*/ |
||
esort: procedure expose |
esort: procedure expose @.; parse arg N; h=N /*get the item count*/ |
||
do while h>1; h=h%2 |
do while h>1; h=h%2 /*partition array. */ |
||
do i=1 for N-h; j=i; k=h+i |
do i=1 for N-h; j=i; k=h+i |
||
do while |
do while @.k<@.j |
||
parse value |
parse value @.j @.k with @.k @.j /*swap two elements.*/ |
||
if h>=j then leave; j=j-h; k=k-h |
if h>=j then leave; j=j-h; k=k-h |
||
end /*while |
end /*while @.k<@.j*/ |
||
end /*i*/ |
end /*i*/ |
||
end /*while h>1*/ |
end /*while h>1*/ |
||
return</lang> |
return</lang> |
||
'''output''' |
'''output''' |
||
<pre> |
|||
<pre style="overflow:scroll"> |
|||
original = 1 1 2 5 15 52 203 877 4140 21147 115975 1 -1 1 0 -1 0 1 0 -1 0 5 0 -691 0 7 0 -3617 3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90 |
original = 1 1 2 5 15 52 203 877 4140 21147 115975 1 -1 1 0 -1 0 1 0 -1 0 5 0 -691 0 7 0 -3617 3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90 |
||
sorted = -3617 -691 -1 -1 -1 0 0 0 0 0 0 0 0 1 1 1 1 1 2 2 2 3 3 5 5 5 5 7 7 10 12 15 17 22 29 39 51 52 68 90 203 877 4140 21147 115975 |
sorted = -3617 -691 -1 -1 -1 0 0 0 0 0 0 0 0 1 1 1 1 1 2 2 2 3 3 5 5 5 5 7 7 10 12 15 17 22 29 39 51 52 68 90 203 877 4140 21147 115975 |