faqts : Computers : Programming : Languages : Tse : Sort

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

Entry

TSE: String: Sort: How to insert single characters in a string while keeping all characters sorted?

Nov 7th, 2006 11:13
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 07 November 2006 - 07:46 pm -------------------

TSE: String: Sort: How to insert single characters in a string while 
keeping all characters sorted?

---

A sorted string means here that all characters are ordered from lowest
to highest ascii value.

 E.g. in the string

  "ABCDEFG"

the characters are ordered,
but the string

  "EGCBAFD"

the characters are not ordered.

1. -If you want to insert a new character in an already sorted string,
    there are always exactly 2 possibilities

    1. -The new character has an ascii value lower than or equal to
        some character in that ordered string

        In that case you insert the new character consequently before
        the higher or equal to ascii value

        E.g.

         If you insert "E" in the string "ABCDFG", and you want to keep
         the string sorted, you find after taking out the characters
         from that string one by one, starting from the beginning, and
         compare each character with the "E", that the character "F"
         has an ascii value higher. So you insert the character "E"
         before that. You do that by splitting the give string "ABCDFG"
         in 2 parts. The first part before the "F", the other part
         after and included the "F"

          "ABCD" + "E" + "F"

         gives thus finally

          "ABCDEF"

    2. -The new character does not have an ascii value lower than or
        equal to some character in that ordered string

        If you do not find any higher ascii value in the string, the
        new character must have the highest ASCII value of them all, so
        you put it at the highest ascii value position, which is at the
        end of the string

        E.g.

         If you insert "Z" in the string "ABCDFG", and you want to keep
         the string sorted, you find after taking out the characters
         from that string one by one, starting from the beginning, and
         compare each character with the "Z", that none of the
         characters has an ascii value higher. So you insert the
         character "Z" at the highest position, which is at the end of
         the string.

          "ABCDFG" + "Z"

         gives thus finally

          "ABCDFGZ"

--- cut here: begin --------------------------------------------------

// library: string: get: sort: character: insert [kn, ho, tu, 07-11-
2006 19:38:47]

STRING PROC FNStringGetSortCharacterInsertS( STRING characterS, STRING 
inS )

 INTEGER I = 0
 INTEGER insertB = FALSE
 INTEGER lengthI = 0
 STRING cS[255] = ""
 STRING s[255] = inS
 I = 1 - 1
 REPEAT
  I = I + 1
  lengthI = Length( s )
  insertB = FALSE
  cS = SubStr( s, I, 1 )
  IF ( characterS <= cS )

   s = SubStr( s, 1, I - 1 ) + characterS + SubStr( s, I, lengthI - I 
+ 1 )

   insertB = TRUE
  ENDIF
 UNTIL ( insertB ) OR ( I >= lengthI )
 IF NOT insertB
  s = s + characterS
 ENDIF
 RETURN( s )
END

PROC Main()

 Message( FNStringGetSortCharacterInsertS( "E", "ABCDFG" ) ) // gives 
e.g. "ABCDEFG"

 Warn( FNStringGetSortCharacterInsertS( "E", "ABCDFG" ) ) // gives 
e.g. "ABCDEFG"

 Warn( FNStringGetSortCharacterInsertS( "Z", "ABCDFG" ) ) // gives 
e.g. "ABCDFGZ"

 Warn( FNStringGetSortCharacterInsertS( "", "ABCDFG" ) ) // gives 
e.g. "ABCDFG"

 Warn( FNStringGetSortCharacterInsertS( "A", "ABCDFG" ) ) // gives 
e.g. "AABCDFG"

END

<F12> Main()

--- cut here: end ----------------------------------------------------

===

Internet: see also:

---



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