Announcement

Collapse
No announcement yet.

Sorting letters in a word

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sorting letters in a word

    I am (re)writing a program in CC. What I need to do is sort the
    individual letters (upper case only) of a word. Example, sort:
    MEL into ELM.

    Currently, I set up a for-next loop and separate the letters
    into an array, ARRAY SORT the array then concanotate the
    letters back into the (a) sorted word. This works but it's
    horribly inefficient since I am looking at hundreds of
    thousands of words to process.

    Any body have any ideas for a more efficient method?

    Cheers


    ------------------
    There are no atheists in a fox hole or the morning of a math test.
    If my flag offends you, I'll help you pack.

  • #2
    This is fast, but if you have to process so many words, then a
    procedure written in assembler will be needed.
    Code:
    SUB SortString (xstr AS STRING)
    
       LOCAL i AS LONG, j AS LONG
       LOCAL ch AS STRING
    
       FOR i = 1 TO LEN(xstr) - 1
          FOR j = 1 TO LEN(xstr) - 1
             IF ASC(xstr, j) > ASC(xstr, j + 1) THEN
                ch = MID$(xstr, j, 1)
                MID$(xstr, j , 1) = MID$(xstr, j + 1, 1)
                MID$(xstr, j + 1, 1) = ch
             END IF
          NEXT
       NEXT
    
    END SUB
    
    FUNCTION PBMAIN
    
       LOCAL xstr AS STRING
    
       xstr = "JEIDLOFNA"
       SortString xstr
       PRINT xstr
       WAITKEY$
       
    END FUNCTION

    ------------------




    [This message has been edited by JOSE ROCA (edited October 18, 2003).]

    Comment


    • #3
      Following should be fast enough - all you need is a string address.
      Code:
        ' txt is word/string to be sorted
        IF LEN(txt) THEN
            REDIM byteStr(LEN(txt) - 1) AS LOCAL BYTE AT STRPTR(txt)
            ARRAY SORT byteStr(0) FOR LEN(txt)
        END IF
      ' added test for string length..

      ------------------
      http://www.tolkenxp.com/pb
      Download: incLean, PBcodec, custom controls and code, etc.
      Borje Hagsten - [email protected]



      [This message has been edited by Borje Hagsten (edited October 18, 2003).]

      Comment

      Working...
      X