Sort an integer array: Difference between revisions

m
→‎sort an array: added/changed whitespace, used a sparse array, eliminated zero-valued array elements.
m (→‎sort an array: removed STYLE from a PRE html tag.)
m (→‎sort an array: added/changed whitespace, used a sparse array, eliminated zero-valued array elements.)
Line 1,241:
===sort an array===
This REXX version creates an array with over a score of Euler numbers (integers), then sorts it.
<lang rexx>/*REXX program to sortsorts (E-sort) an arrayarra y (which contains integers). */
numeric digits 30 numeric digits 30 /*handle larger Euler numbers. */
a @.1 = 10 /*default for all Euler numbers. */
a @.21= 01
a @.3= -1
a @.45= 05
a.5= 5 @.7= -61
a.6= 0 @.9= 1385
a.7= @.11= -6150521
a.8= 0 @.13= 2702765
a.9= 1385 @.15= -199360981
a.10= 0 @.17= 19391512145
a.11= @.19= -505212404879675441
a.12= 0 @.21= 370371188237525
size=21 /*haveindicate athere list ofare 21 Euler numbers#'s*/
a.13= 2702765
call tell 'un-sorted' /*display the array before sort. */
a.14= 0
call esort size /*sort the array of Euler numbers*/
a.15= -199360981
call tell ' sorted' /*display the array after sort. */
a.16= 0
a.17= 19391512145
a.18= 0
a.19= -2404879675441
a.20= 0
a.21= 370371188237525
 
size=21 /*have a list of 21 Euler numbers*/
call tell 'un-sorted'
call esort size
call tell ' sorted'
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────ESORT subroutine────────────────────*/
esort: procedure expose a@.; parse arg N; h=N
do while h>1; h=h%2
do i=1 for N-h; j=i; k=h+i
do while a @.k<a@.j
parse value a @.j a@.k with a @.k a@.j /*swap two elements.*/
if h>=j then leave; j=j-h; k=k-h
end /*while a@.k<a@.j*/
end /*i*/
end /*while h>1*/
Line 1,283 ⟶ 1,273:
/*──────────────────────────────────TELL subroutine─────────────────────*/
tell: say center(arg(1),50,'─')
do j=1 for size
say arg(1) 'array element' right(j,length(size))'='right(a@.j,20)
end /*j*/
say