Sorting a text array.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Gray

    Sorting a text array.

    Greetings all,

    I need to sort an array containing text only values and remove
    duplicates at the same time. I was thinking of...

    1. Loading all values into one array (Array1)

    2. Read through this array and inserting values into a new array
    (Array2) only if they don't exist in the second array.

    3. Performing a bubble sort of the unique values in array2

    Is there an easier of doing this?

    Regards,
    Dave.

  • Cookie

    #2
    Re: Sorting a text array.

    Cookie wrote:[color=blue]
    > Stephane Richard wrote:
    >[color=green]
    >> You can save time by adding items to array 1 only if they dont alread
    >> exist.
    >> Then you sort the array, bubble sort or quick sort (I beleive the
    >> latter is
    >> faster)[/color]
    >
    > First things first, you mean array 2 :-)
    >
    >
    > Well, this is actualy the slowest method :-)
    > Because you have to check whole array 2 for existing elements. Why?
    > Because array 1 isn't sorted yet....
    >
    > The right method:
    > sort array 1
    > THEN add the elements to array 2 skipping the element if it is the same
    > as the previous one...
    > (no whole array-checking needed here)
    >
    > ie.
    > Array 1:
    > 8 6 3 4 8 5 2 1 9 4 6 2
    > Sorted Array1:
    > 1 2 2 3 4 4 5 6 6 8 8 9
    > Put it in array two (skipping elements if they are the same as previous
    > one):
    > 1 2 (2) 3 4 (4) 5 6 (6) 8 (8) 9 (elements between () are skipped)[/color]

    One more thing:
    You even don't have to have 2 arrays with this method:
    Sorted Array1:
    1 2 2 3 4 4 5 6 6 8 8 9

    Now shift all element to the left if they are the same:
    1 2 2 3 4 4 5 6 6 8 8 9
    1 2 3 4 4 5 6 6 8 8 9 *
    1 2 3 4 5 6 6 8 8 9 * *
    1 2 3 4 5 6 8 8 9 * * *
    1 2 3 4 5 6 8 9 * * * *

    Example program:
    -------------------------
    DIM Array(1 TO ???)

    Sort_The_Array_ Here

    I = 1
    FOR T = 2 TO UBOUND(Array)
    IF Array(T) <> Array(T - 1) THEN
    I = I + 1
    Array(I) = Array(T)
    ENDIF
    NEXT T

    New dimension for the array is now: 1 TO I
    -------------------------


    --
    \|||/
    (. .)
    *--------ooO-(_)-Ooo--------*
    | |
    | cookie@yucom.be |
    | |
    *---------------------------*

    Comment

    • David Gray

      #3
      Re: Sorting a text array.

      Ok please excuse my ignorance but what is a quick sort?


      On Mon, 18 Aug 2003 16:41:59 GMT, "Stephane Richard"
      <stephane.richa rd@verizon.net> wrote:
      [color=blue]
      >You can save time by adding items to array 1 only if they dont alread exist.
      >Then you sort the array, bubble sort or quick sort (I beleive the latter is
      >faster)[/color]

      Comment

      • David Gray

        #4
        Re: Sorting a text array.

        Hi,

        Agreed, sorting first would be more efficient. I like the idea of
        just moving the elements one space left if they are the same as the
        last.

        Thanks for your help and the example.

        Dave.




        On Tue, 19 Aug 2003 00:23:03 +0200, Cookie <cookie@yucom.b e> wrote:
        [color=blue]
        >Cookie wrote:[color=green]
        >> Stephane Richard wrote:
        >>[color=darkred]
        >>> You can save time by adding items to array 1 only if they dont alread
        >>> exist.
        >>> Then you sort the array, bubble sort or quick sort (I beleive the
        >>> latter is
        >>> faster)[/color]
        >>
        >> First things first, you mean array 2 :-)
        >>
        >>
        >> Well, this is actualy the slowest method :-)
        >> Because you have to check whole array 2 for existing elements. Why?
        >> Because array 1 isn't sorted yet....
        >>
        >> The right method:
        >> sort array 1
        >> THEN add the elements to array 2 skipping the element if it is the same
        >> as the previous one...
        >> (no whole array-checking needed here)
        >>
        >> ie.
        >> Array 1:
        >> 8 6 3 4 8 5 2 1 9 4 6 2
        >> Sorted Array1:
        >> 1 2 2 3 4 4 5 6 6 8 8 9
        >> Put it in array two (skipping elements if they are the same as previous
        >> one):
        >> 1 2 (2) 3 4 (4) 5 6 (6) 8 (8) 9 (elements between () are skipped)[/color]
        >
        >One more thing:
        >You even don't have to have 2 arrays with this method:
        >Sorted Array1:
        >1 2 2 3 4 4 5 6 6 8 8 9
        >
        >Now shift all element to the left if they are the same:
        >1 2 2 3 4 4 5 6 6 8 8 9
        >1 2 3 4 4 5 6 6 8 8 9 *
        >1 2 3 4 5 6 6 8 8 9 * *
        >1 2 3 4 5 6 8 8 9 * * *
        >1 2 3 4 5 6 8 9 * * * *
        >
        >Example program:
        >-------------------------
        >DIM Array(1 TO ???)
        >
        >Sort_The_Array _Here
        >
        >I = 1
        >FOR T = 2 TO UBOUND(Array)
        > IF Array(T) <> Array(T - 1) THEN
        > I = I + 1
        > Array(I) = Array(T)
        > ENDIF
        >NEXT T
        >
        >New dimension for the array is now: 1 TO I
        >-------------------------[/color]

        Comment

        • David Gray

          #5
          Re: Sorting a text array.

          Thanks to all who replied from your input.

          Cheers,
          Dave.

          On Mon, 18 Aug 2003 16:41:59 GMT, "Stephane Richard"
          <stephane.richa rd@verizon.net> wrote:
          [color=blue]
          >You can save time by adding items to array 1 only if they dont alread exist.
          >Then you sort the array, bubble sort or quick sort (I beleive the latter is
          >faster)[/color]

          Comment

          Working...