English
Source / code snippets

Speicherminimaler Teilsort for Integerarrays: The Singleton-Algorithmus

 

p.specht

Zugegeben, nowadys is Speicherminimierung with Rechnern hardly meaningfully, with Embeded Systems (Microprozessoen in Autos, Waschmaschinen, IoT-Geräten etc.) becomes though still grosser worth hereon laid. moreover here quite populärer Algorithmus, naturally How always as private XProfan-11-demonstration without Gewähr...The rights lying with ACM.

Note: contains a the simplest Pseudozufallsgeneratoren, The there's. from mathematischer visibility grottenschlecht, for virtually Verhältnisse but thoroughly suitable.
Window Title upper$("Singleton-Sort one Integer-Vektors between index ii and jj")
' (D) DEMO TRANSLATION from Fortran77 to XProfan11.2a in 2014-11
' by P. woodpecker, Wien (Austria); without jedwede Gewähr! No warranties whatsoever!
'*******************************************************************************
' on 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.
'*******************************************************************************
Window Style 24:font 2
Window 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&]
    case A&[I&]<=T&:GOTO "G20"
    A&[IJ&] = A&[I&]
    A&[I&] = T&
    T& = A&[IJ&]
    G20:
    L& = J&
    case A&[J&] >= T& : GOTO "G40"
    A&[IJ&] = A&[J&]
    A&[J&] = T&
    T& = A&[IJ&]
    case 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
    case A&[L&] > T& : GOTO "G40"
    TT& = A&[L&]
    G50:
    K& = K& + 1
    case A&[K&] < T& : GOTO "G50"
    case K& <= L&: GOTO "G30"
    case (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
    case M& = 0 : RETURN
    I& = IL&[M&]
    J& = IU&[M&]
    G80:
    case (J&-I&) >= II& : GOTO "G10"
    case I& = II& : GOTO "G5"
    I& = I& - 1
    G90:
    I& = I& + 1
    case I& = J& : GOTO "G70"
    T& = A&[I&+1]
    case A&[I&] <= T& : GOTO "G90"
    K& = I&
    G100:
    A&[K&+1] = A&[K&]
    K& = K& - 1
    case T& < A&[K&] : GOTO "G100"
    A&[K&+1] = T&
    GOTO "G90"

endproc

Proc RN' uses seed&

    declare k&,rn! : case seed&=0:seed=rnd()
    '*******************************************************************************
    '  RN returns a unit single precision pseudorandom number.
    '*******************************************************************************
    '
    '  Diese routine implements the 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.
    '
    '    If the initial seed is 12345, then the first three computations are
    '
    '      Input     output      RN
    '      SEED      SEED
    '
    '         12345   207482415  0.096616
    '     207482415  1790989824  0.833995
    '    1790989824  2035175616  0.947702
    '
    '  Modified: 11 august 2004, Author: John Burkardt
    '
    '  Reference:
    '
    '    Paul Bratley, Bennett Fox, L E Schrage,
    '    A Guide to Simulation,
    '    floater publisher, pages 201-202, 1983.
    '
    '    Pierre L'Ecuyer,
    '    Random Number generation,
    '    in: Handbook of Simulation,
    '    edited by Jerry Banks,
    '    Wiley Interscience, page 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 the system/360,
    '    IBM Systems journal,
    '    Volume 8, pages 136-143, 1969.
    '
    '  Parameters:
    '
    '    Input/output, integer SEED, the "seed" value, which should NOT be 0.
    '    On output, SEED has been updated.
    '
    '    output, real RN, a new pseudorandom variate,
    '    strictly between 0 and 1.
    '
    k& = seed& \ 127773
    seed& = 16807 * ( seed& - k& * 127773 ) - k& * 2836

    if seed& < 0

        seed& = seed& + 2147483647

    endif

    '  Although SEED can be represented exactly as a 32 bit integer,
    '  it generally cannot be represented exactly as a 32 bit real number'
    rn! = seed& * val("4.656612875E-10")
    return rn!

endproc

' MAIN tests SORT.
' Modified 04 January 2006 by John Burkardt
Print "\n\n TOMS347_PRB\n Test Singleton Sort, which sorts on integer vector ascending."
declare ii&,jj&
ii& = 1
jj& = 20
Test01(ii&,jj&)
waitinput
ii& = 5
jj& = 18
Test01(ii&,jj&)
print "\n TOMS347_PRB: Success - normal end of execution!"
waitinput
end

Proc Test01 :parameters ii&,jj&

    '*******************************************************************************
    '  TEST01 tests SORT on a particular brat of indices.
    '  Modified 04 January 2006 by John Burkardt
    '*******************************************************************************
    var n&=20
    declare a&[n&],i&,rn!,seed&
    Print "\n\n TEST01: Ascending sorts on integer vector."
    Print " hier we sort entries II = ";ii&;" to JJ = ";jj&
    seed& = 123456789

    whileloop n& : i&=&Loop

        a&[i&] = int(n&*RN(seed&))

    endwhile

    Print "\n Unsorted aray:"

    whileloop n&:i&=&Loop

        print i&, a&[i&]

    endwhile

    SingletonSORT(a&[],ii&,jj&)
    Print "\n Sorted aray:"

    whileloop n&:i&=&Loop

        print i&, a&[i&]

    endwhile

endproc

''TOMS347_PRB  RESULTS OF Test SORT, which ascending sorts on integer vector.
'
'TEST01
'  SORT ascending sorts on integer vector.
'  hier we sort entries II =      1
'  through JJ =     20
'
'  Unsorted aray:
'
'       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 aray:
'
'       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 on integer vector.
'  hier we sort entries II =      5
'  through JJ =     18
'
'  Unsorted aray:
'
'       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 aray:
'
'       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'...
05/17/21  
 



Zum Quelltext


Topictitle, max. 100 characters.
 

Systemprofile:

no Systemprofil laid out. [anlegen]

XProfan:

 Posting  Font  Smilies  ▼ 

Please register circa a Posting To verfassen.
 

Topic-Options

829 Views

Themeninformationen

this Topic has 1 subscriber:

p.specht (1x)


Admins  |  AGB  |  Applications  |  Authors  |  Chat  |  Privacy Policy  |  Download  |  Entrance  |  Help  |  Merchantportal  |  Imprint  |  Mart  |  Interfaces  |  SDK  |  Services  |  Games  |  Search  |  Support

One proposition all XProfan, The there's!


My XProfan
Private Messages
Own Storage Forum
Topics-Remember-List
Own Posts
Own Topics
Clipboard
Log off
 Deutsch English Français Español Italia
Translations

Privacy Policy


we use Cookies only as Session-Cookies because of the technical necessity and with us there no Cookies of Drittanbietern.

If you here on our Website click or navigate, stimmst You ours registration of Information in our Cookies on XProfan.Net To.

further Information To our Cookies and moreover, How You The control above keep, find You in ours nachfolgenden Datenschutzerklärung.


all rightDatenschutzerklärung
i want none Cookie