Problem

A value list, collected in the stack buffer has to be sorted ascending. Doubles have to be eliminated.

Solution

It is possible to loop through the buffer by PUT and GET. The algorithm compares in each loop 2 values and switches them (srt=1) if not in order. If all are sorted srt=0.

Script

abweichung=0.0001 !»» tolerance for comparation

PUT 1,4,7,2,4,3,9 !»» values on stack
GOSUB 100 !»» call sort algorithm

IF nsp THEN VALUES "var" GET(nsp), CUSTOM

END

! ### Sort buffer and ignore doubles
100:
DO
  srtn=nsp : srt=0 : i=1
  WHILE i<=srtn AND srtn>1
AND nsp DO
    IF i=1 THEN
      srt1=get(1) !»» first value
      ELSE
      srt2=get(1)
     
IF ABS(srt1-srt2)<abweichung THEN
        !»» values are identical

        WHILE ABS(srt1-srt2)<abweichung AND nsp DO
          i=i+1 : srt2=get(1) !»» pull next one
          ENDWHILE
        ENDIF
     
IF srt1<srt2 THEN
        PUT srt1 !»» First first
       
ELSE
        IF srt1>srt2 THEN
          PUT srt2 !»» Second first
          srt2=srt1 : srt=1
         
ENDIF
        ENDIF
      srt1=srt2 !»» Prepare next round
     
ENDIF
    i=i+1
   
ENDWHILE
  PUT srt2 !»» Rescue last one
  WHILE srt AND nsp>1
RETURN

Um unsere Webseite für Sie optimal zu gestalten und fortlaufend verbessern zu können, verwenden wir nur essenzielle Cookies.
Durch die weitere Nutzung der Webseite stimmen Sie der Verwendung von Cookies zu.
Weitere Informationen zu Cookies erhalten Sie in unserer Datenschutzerklärung