469,082 Members | 1,154 Online

# 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...

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.

Jul 17 '05 #1
4 10433
Stephane Richard wrote:
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)

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)

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--------*
| |
| co****@yucom.be |
| |
*---------------------------*

Jul 17 '05 #2
Ok please excuse my ignorance but what is a quick sort?
On Mon, 18 Aug 2003 16:41:59 GMT, "Stephane Richard"
<st**************@verizon.net> wrote:
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)

Jul 17 '05 #3
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 <co****@yucom.be> wrote:
Stephane Richard wrote:
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)

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)

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
-------------------------

Jul 17 '05 #4
Thanks to all who replied from your input.

Cheers,
Dave.

On Mon, 18 Aug 2003 16:41:59 GMT, "Stephane Richard"
<st**************@verizon.net> wrote:
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)

Jul 17 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.