Home All Groups Group Topic Archive Search About
Author
18 Oct 2005 7:31 PM
abcd
I am looking for the open source for Merging 2 arrays in VB.

I have 2 arrays like

Dim x(2) As Integer
Dim y(3) As Integer
x(0) = 1
x(1) = 2
x(2) = 3

y(0) = 4
y(1) = 5
y(2) = 3
y(3) = 1

I want my final array as

k(0) = 1
k(1) = 2
k(2) = 3
k(3) = 4
k(4) = 5

Does anybody know the open source code that I can use.

thanks in advance

Author
18 Oct 2005 8:02 PM
Duane Bozarth
abcd wrote:
>
> I am looking for the open source for Merging 2 arrays in VB.
....

That's simply allocating a new array, copying the two into it and
sorting the result...

You could, alternatively, sort each array and work through them
comparing elements to form the merge, but the first is conceptually
simpler and unless the merged array is quite large, possibly at least as
fast.

There are a zillion sort routines available a google search will find in
VB source.
Author
18 Oct 2005 8:09 PM
Jeff Johnson [MVP: VB]
"abcd" <a***@abcd.com> wrote in message
news:u5%23r9pB1FHA.1256@TK2MSFTNGP09.phx.gbl...

>I am looking for the open source for Merging 2 arrays in VB.

It's not necessary to ask for "open source." If someone posts code here in a
group or on the 'Net it's pretty much open unless otherwise specified, and
even then you can read it, learn from it, and "do it yourself."

Show quoteHide quote
> I have 2 arrays like
>
> Dim x(2) As Integer
> Dim y(3) As Integer
> x(0) = 1
> x(1) = 2
> x(2) = 3
>
> y(0) = 4
> y(1) = 5
> y(2) = 3
> y(3) = 1
>
> I want my final array as
>
> k(0) = 1
> k(1) = 2
> k(2) = 3
> k(3) = 4
> k(4) = 5
>
> Does anybody know the open source code that I can use.

What you really mean is that you want to take two arrays, determine the
UNIQUE values in them, and then create a new array with those values in a
sorted order. Not exactly the plain-vanilla definition of "merge."

Off the top of my head I recommend a two-step approach: Begin adding values
from the first array to the new array, all the while checking the you're not
adding a duplicate (this will require looping through the array a lot) and
then doing the same with the second array. After that, sort it. You can find
a bazillion (or is that brazillian?) examples of sorting out there.

There may be an easier way involving hash tables, so look them up too. I
just don't use them much myself.
Author
18 Oct 2005 8:35 PM
Duane Bozarth
"Jeff Johnson [MVP: VB]" wrote:
>
> "abcd" <a***@abcd.com> wrote in message
> news:u5%23r9pB1FHA.1256@TK2MSFTNGP09.phx.gbl...
>
> >I am looking for the open source for Merging 2 arrays in VB.

....snip example of selective merge...

> What you really mean is that you want to take two arrays, determine the
> UNIQUE values in them, and then create a new array with those values in a
> sorted order. Not exactly the plain-vanilla definition of "merge."
>
> Off the top of my head I recommend a two-step approach: Begin adding values
> from the first array to the new array, all the while checking the you're not
> adding a duplicate (this will require looping through the array a lot) and
> then doing the same with the second array. After that, sort it. You can find
> a bazillion (or is that brazillian?) examples of sorting out there.
>
> There may be an easier way involving hash tables, so look them up too. I
> just don't use them much myself.

I hadn't noted the "incomplete" merge previously--if the two arrays are
sorted initially one can minimize the number of loops through the second
for the comparison as once once finds a match one can stop and then pick
up from that point on the next.  It also would eliminate the need to
sort the new array.

Or, one could simply merge, sort, then toss out the duplicates by
finding the sequential elements which have a difference between adjacent
values of zero.

Which would be faster would depend on the sizes of the arrays and the
charactreristics of the data.
Author
18 Oct 2005 9:06 PM
Someone
> k(0) = 1
> k(1) = 2
> k(2) = 3
> k(3) = 4
> k(4) = 5

If you really want the array to contain 5 elements only instead of 7, see
"ReDim Statement" in MSDN. You could define it as 7 elements then shrink it
to 5 later.

Besides what others suggested, you could loop through X and Y arrays and
find the smallest number and put it in K. Loop again and find a number
bigger than the last one and so on. This way you don't have to sort the K
array when you are done.



Show quoteHide quote
"abcd" <a***@abcd.com> wrote in message
news:u5%23r9pB1FHA.1256@TK2MSFTNGP09.phx.gbl...
>I am looking for the open source for Merging 2 arrays in VB.
>
> I have 2 arrays like
>
> Dim x(2) As Integer
> Dim y(3) As Integer
> x(0) = 1
> x(1) = 2
> x(2) = 3
>
> y(0) = 4
> y(1) = 5
> y(2) = 3
> y(3) = 1
>
> I want my final array as
>
> k(0) = 1
> k(1) = 2
> k(2) = 3
> k(3) = 4
> k(4) = 5
>
> Does anybody know the open source code that I can use.
>
> thanks in advance
>
>
>
Author
18 Oct 2005 9:39 PM
abcd
Thanks ALL

I took this approach

arr1 and arr2 are say 2 arrays

Algorithm

1. Filter arr2 from arr1 and copy the result into temp array. If both array
are same simpy return arr2 or arr1.
2. copy all elements of arr2 and temp array to form my final array
3. apply bubblesort on my final array

thanks



abcd wrote:
Show quoteHide quote
> I am looking for the open source for Merging 2 arrays in VB.
>
> I have 2 arrays like
>
> Dim x(2) As Integer
> Dim y(3) As Integer
> x(0) = 1
> x(1) = 2
> x(2) = 3
>
> y(0) = 4
> y(1) = 5
> y(2) = 3
> y(3) = 1
>
> I want my final array as
>
> k(0) = 1
> k(1) = 2
> k(2) = 3
> k(3) = 4
> k(4) = 5
>
> Does anybody know the open source code that I can use.
>
> thanks in advance