' *******************
' *** SORTSTR.LST ***
' *******************
'
DEFWRD "a-z"
'
> PROCEDURE bin.search.string(element$,VAR proc$(),index)
  ' *** find element$ in sorted string array (binary search)
  ' *** global :   FOUND!
  LOCAL first,last,middle
  first=1
  last=DIM?(proc$())-1
  WHILE first<last
    middle=DIV(ADD(first,last),2)
    IF element$>proc$(middle)
      first=ADD(middle,1)
    ELSE
      last=middle
    ENDIF
  WEND
  found!=(proc$(first)=element$)
  IF found!
    index=first
  ELSE
    index=0
  ENDIF
RETURN
' **********
'
> PROCEDURE ascii.qsort(VAR txt$())
  ' *** 'true' alphabetical sorting of string-array
  IF DIM?(ascii|())=0
    @initio.ascii.array
  ENDIF
  QSORT txt$() WITH ascii|()
RETURN
' ***
> PROCEDURE initio.ascii.array
  ' *** ASCII byte-array to be used with QSORT (or SSORT)
  ' *** global :  ASCII|()
  LOCAL i,code1,code2
  DIM ascii|(255)
  ARRAYFILL ascii(),32           ! fill with space-character
  FOR i=48 TO 57
    ascii|(i)=i                  ! 0 - 9
  NEXT i
  FOR i=65 TO 90
    ascii|(i)=i                  ! A - Z
  NEXT i
  FOR i=97 TO 122
    ascii|(i)=SUB(i,32)          ! a - z, converted to A - Z
  NEXT i
  RESTORE ascii.data
  REPEAT
    READ code1,code2
    ascii|(code1)=code2
  UNTIL code1=0
  '
  ascii.data:
  ' *** format : ASCII-code,replacement
  DATA 128,67,129,85,130,69,131,65,132,65,133,65,134,65,135,67,136,69,137,69
  DATA 138,69,139,73,140,73,141,73,142,65,143,65,144,69,145,65,146,65,147,79
  DATA 148,79,149,79,150,85,151,85,152,121,153,79,154,85,155,67,158,83,160,65
  DATA 161,73,162,79,163,85,164,78,165,78,166,65,167,79,176,65,177,79,178,79
  DATA 179,79,180,79,181,79,182,65,183,65,184,79,192,121,193,121,225,83,0,0
RETURN
' **********
'
> PROCEDURE string.index.qsort(switch!,VAR txt$(),index%())
  ' *** fills index-array with index-numbers of sorted string-array
  ' *** string-array txt$() is not changed !
  ' *** the index-array has to exist already (DIM before calling this Procedure)
  ' *** if switch!=TRUE, array ascii|() is used for 'true' alphabetical sorting
  ' *** all elements (0 - last) are sorted, element txt$(0) is NOT ignored !!
  ' *** index-array should be used like this :
  '                   FOR i=0 TO last
  '                     PRINT txt$(index%(i))
  '                   NEXT i
  LOCAL last,i
  last=DIM?(txt$())-1           ! index of last element
  DIM temp$(last)
  FOR i=0 TO last
    temp$(i)=txt$(i)
  NEXT i
  FOR i=0 TO last
    index%(i)=i
  NEXT i
  IF switch!
    IF DIM?(ascii|())=0
      @initio.ascii.array
    ENDIF
    QSORT temp$() WITH ascii|(),last+1,index%()
  ELSE
    QSORT temp$(),last+1,index%()
  ENDIF
  ERASE temp$()
RETURN
' **********
'
