Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default Mergeing two Arrays

This will merge two sorted arrays of variant. If you want another data
type, just dimension A1 to A3 accordingly.

Dave Ring

Sub Merge(A1(), A2(), A3())
Dim I&, J&, K&, N1&, N2&, N3&

'merges sorted arrays A1 and A2 into A3
N1 = UBound(A1) + 1 'if array is 0 based
N2 = UBound(A2) + 1
N3 = N1 + N2
ReDim A3(0 To N3 - 1) 'again, 0 based
I = 0: J = 0: K = 0
Do
If A1(I) <= A2(J) Then
A3(K) = A1(I): I = I + 1: K = K + 1
If I = N1 Then 'A1 is used up, so
Do 'move the rest of A2
A3(K) = A1(J): J = J + 1: K = K + 1
Loop Until J = N2: Exit Do
End If
Else
A3(K) = A1(J): J = J + 1: K = K + 1
If J = N2 Then 'A2 is used up, so
Do 'move the rest of A1
A3(K) = A1(I): I = I + 1: K = K + 1
Loop Until I = N1: Exit Do
End If
End If
Loop
End Sub


Paul W Smith wrote:
Has anyone got any sample code for merging two sorted one dimensional arrays
into one sorted new one?

e.g.

A1 = (2, 4, 6, 8)
A2 = (2, 3, 4, 9, 10)

RESULT = (2, 3, 4, 6, 8, 9, 10)



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mergeing Cells maths15 Excel Worksheet Functions 4 April 7th 08 11:02 PM
mergeing cells and wrapping text leo Excel Discussion (Misc queries) 2 March 21st 07 06:51 PM
Mergeing two Arrays Dave Ring Excel Programming 0 October 1st 03 01:13 AM
Mergeing two Arrays Dave Peterson[_3_] Excel Programming 0 September 30th 03 02:30 AM
Mergeing two Arrays merjet Excel Programming 0 September 30th 03 02:24 AM


All times are GMT +1. The time now is 05:33 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"