| |
|
|
p.specht
| Zugegeben, heutzutage ist Speicherminimierung chez Rechnern à peine sinnvoll, chez Embeded Systems (Microprozessoen dans Autos, Waschmaschinen, IoT-Geräten etc.) wird allerdings toujours grosser Wert puis gelegt. en supplément ici un assez populärer Algorithmus, naturellement comment toujours comme private XProfan-11-Demo sans Gewähr...qui Rechte liegen chez ACM.
Anmerkung: Enthält une qui einfachsten Pseudozufallsgeneratoren, qui il y a. Aus mathematischer Sicht grottenschlecht, pour praktische Verhältnisse mais durchaus approprié.
Titre de la fenêtre upper$("Singleton-Sort eines Integer-Vektors entre index ii et jj")
' (D) DEMO TRANSLATION à partir de Fortran77 to XProfan11.2a dans 2014-11
' by P. Specht, vienne (Austria); sans jedwede Gewähr! No warranties whatsoever!
'*******************************************************************************
' à Efficient Algorithm for Sorting with Minimal Storage, by: R. C. Singleton
' ALGORITHM 347, COLLECTED ALGORITHMS FROM ACM.
' THIS WORK PUBLISHED IN COMMUNICATIONS OF THE ACM
' VOL. 12, NO. 3, March, 1969, PP.185--187.
'*******************************************************************************
' SORTS ARRAY A INTO INCREASING ORDER, FROM A(II) TO A(JJ).
' ORDERING IS BY ***INTEGER SUBTRACTION***, THUS FLOATING POINT
' NUMBERS MUST BE IN NORMALIZED FORM!
' ARRAYS IU(K) AND IL(K) PERMIT SORTING UP TO 2^(K+1)-1 ELEMENTS.
'*******************************************************************************
Fenêtre Style 24:font 2
Fenêtre 0,0-%maxx,%maxy
Proc SingletonSORT :parameters A&[],ii&,jj&
DECLARE iJ&,il&[16],iu&[16],j&,k&,l&,m&,t&,tt&
M& = 1
I& = II&
J& = JJ&
G5:
CASE I& >= J&: GOTO "G70"
G10:
K& = I&
IJ& = (J&+I&)/2
T& = A&[IJ&]
cas A&[I&]<=T&:GOTO "G20"
A&[IJ&] = A&[I&]
A&[I&] = T&
T& = A&[IJ&]
G20:
L& = J&
cas A&[J&] >= T& : GOTO "G40"
A&[IJ&] = A&[J&]
A&[J&] = T&
T& = A&[IJ&]
cas A&[I&] <= T& : GOTO "G40"
A&[IJ&] = A&[I&]
A&[I&] = T&
T& = A&[IJ&]
GOTO "G40"
G30:
A&[L&] = A&[K&]
A&[K&] = TT&
G40:
L& = L& - 1
cas A&[L&] > T& : GOTO "G40"
TT& = A&[L&]
G50:
K& = K& + 1
cas A&[K&] < T& : GOTO "G50"
cas K& <= L&: GOTO "G30"
cas (L&-I&) <= (J&-K&): GOTO "G60"
IL&[M&] = I&
IU&[M&] = L&
I& = K&
M& = M& + 1
GOTO "G80"
G60:
IL&[M&] = K&
IU&[M&] = J&
J& = L&
M& = M& + 1
GOTO "G80"
G70:
M& = M& - 1
cas M& = 0 : RETOUR
I& = IL&[M&]
J& = IU&[M&]
G80:
cas (J&-I&) >= II& : GOTO "G10"
cas I& = II& : GOTO "G5"
I& = I& - 1
G90:
I& = I& + 1
cas I& = J& : GOTO "G70"
T& = A&[I&+1]
cas A&[I&] <= T& : GOTO "G90"
K& = I&
G100:
A&[K&+1] = A&[K&]
K& = K& - 1
cas T& < A&[K&] : GOTO "G100"
A&[K&+1] = T&
GOTO "G90"
endproc
Proc RN' uses seed&
declare k&,rn! : cas seed&=0:seed=rnd()
'*******************************************************************************
' RN returns a unit single precision pseudorandom number.
'*******************************************************************************
'
' This routine implements le recursion
'
' seed = 16807 * seed mod ( 2^31 - 1 )
' rn = seed / ( 2**31 - 1 )
'
' The integer arithmetic never requires more than 32 bits, including a sign bit.
'
' Si le initial seed is 12345, then le first three computations sont
'
' Contribution Output RN
' SEED SEED
'
' 12345 207482415 0.096616
' 207482415 1790989824 0.833995
' 1790989824 2035175616 0.947702
'
' Modified: 11 août 2004, Author: John Burkardt
'
' Reference:
'
' Paul Bratley, Bennett Fox, L E Schrage,
' A Guide to Simulation,
' Springer Verlag, pages 201-202, 1983.
'
' Pierre L'Ecuyer,
' Random Number Generation,
' dans: Handbook of Simulation,
' edited by Jerry Banks,
' Wiley Interscience, le chasseur 95, 1998.
'
' Bennett Fox,
' Algorithm 647:
' Implementation and Relative Efficiency of Quasirandom
' Sequence Generators,
' ACM Transactions on Mathematical Software,
' Volume 12, Number 4, pages 362-376, 1986.
'
' P A Lewis, A S Goodman, J M Miller,
' A Pseudo-Random Number Generator for le System/360,
' IBM Systems journal,
' Volume 8, pages 136-143, 1969.
'
' Paramètres:
'
' Contribution/output, integer SEED, le "seed" value, which should NOT être 0.
' On output, SEED has been updated.
'
' Output, réel RN, a new pseudorandom variate,
' strictly between 0 and 1.
'
k& = seed& \ 127773
seed& = 16807 * ( seed& - k& * 127773 ) - k& * 2836
si seed& < 0
seed& = seed& + 2147483647
endif
' Although SEED can être represented exactly as a 32 bit integer,
' il generally cannot être represented exactly as a 32 bit réel number'
rn! = seed& * val("4.656612875E-10")
return rn!
endproc
' MAIN tests SORT.
' Modified 04 January 2006 by John Burkardt
Imprimer "\n\n TOMS347_PRB\n Test Singleton Sort, which sorts à integer vector ascending."
declare ii&,jj&
ii& = 1
jj& = 20
test01(ii&,jj&)
waitinput
ii& = 5
jj& = 18
test01(ii&,jj&)
imprimer "\n TOMS347_PRB: Success - Normal end of execution!"
waitinput
end
Proc test01 :parameters ii&,jj&
'*******************************************************************************
' TEST01 tests SORT on a particular la gamine of indices.
' Modified 04 January 2006 by John Burkardt
'*******************************************************************************
var n&=20
declare a&[n&],i&,rn!,seed&
Imprimer "\n\n TEST01: Ascending sorts à integer vector."
Imprimer " Here we sort entries II = ";ii&;" to JJ = ";jj&
seed& = 123456789
whileloop n& : i&=&Boucle
a&[i&] = int(n&*RN(seed&))
endwhile
Imprimer "\n Unsorted array:"
whileloop n&:i&=&Boucle
imprimer i&, a&[i&]
endwhile
SingletonSORT(a&[],ii&,jj&)
Imprimer "\n Sorted array:"
whileloop n&:i&=&Boucle
imprimer i&, a&[i&]
endwhile
endproc
''TOMS347_PRB RESULTS OF Test SORT, which ascending sorts à integer vector.
'
'TEST01
' SORT ascending sorts à integer vector.
' Here we sort entries II = 1
' through JJ = 20
'
' Unsorted array:
'
' 1 4
' 2 19
' 3 16
' 4 11
' 5 8
' 6 1
' 7 5
' 8 2
' 9 0
' 10 12
' 11 1
' 12 8
' 13 8
' 14 15
' 15 15
' 16 0
' 17 17
' 18 7
' 19 1
' 20 0
'
' Sorted array:
'
' 1 0
' 2 0
' 3 0
' 4 1
' 5 1
' 6 1
' 7 2
' 8 4
' 9 5
' 10 7
' 11 8
' 12 8
' 13 8
' 14 11
' 15 12
' 16 15
' 17 15
' 18 16
' 19 17
' 20 19
'
'TEST01
' SORT ascending sorts à integer vector.
' Here we sort entries II = 5
' through JJ = 18
'
' Unsorted array:
'
' 1 4
' 2 19
' 3 16
' 4 11
' 5 8
' 6 1
' 7 5
' 8 2
' 9 0
' 10 12
' 11 1
' 12 8
' 13 8
' 14 15
' 15 15
' 16 0
' 17 17
' 18 7
' 19 1
' 20 0
'
' Sorted array:
'
' 1 4
' 2 19
' 3 16
' 4 11
' 5 0
' 6 0
' 7 1
' 8 1
' 9 2
' 10 5
' 11 7
' 12 8
' 13 8
' 14 8
' 15 12
' 16 15
' 17 15
' 18 17
' 19 1
' 20 0
|
|
|
| Computer: Gerät, daß es in Mikrosekunden erlaubt, 50.000 Fehler zu machen, zB 'daß' statt 'das'... | 17.05.2021 ▲ |
|
|
|