|
|
|
Click here to copy the following block |
Dim S1(L To R) As String Dim P1(L To R) As Long Dim P2(L To R) As Long Dim L1(L To R) As Long Dim L2(L To R) As Long For I = L To R S1(I) = GetRandomString() P1(I) = I L1(I) = GetRandomLong() Next I
pMergeSortS L, R, S1, P1, P2 MergeSortL L, R, L1, L2
Sub pMergeSortS(L As Long, R As Long, A() As String, P() As Long, Q() As Long) Dim LP As Long Dim RP As Long Dim OP As Long Dim MID As Long If R - L < 10 Then pInsertS L, R, A, P Else MID = (L + R) \ 2 pMergeSortS L, MID, A, P, Q pMergeSortS MID + 1, R, A, P, Q LP = L RP = MID + 1 OP = L Do If A(P(LP)) <= A(P(RP)) Then Q(OP) = P(LP) OP = OP + 1 LP = LP + 1 If LP > MID Then Do Q(OP) = P(RP) OP = OP + 1 RP = RP + 1 Loop Until RP > R Exit Do End If Else Q(OP) = P(RP) OP = OP + 1 RP = RP + 1 If RP > R Then Do Q(OP) = P(LP) OP = OP + 1 LP = LP + 1 Loop Until LP > MID Exit Do End If End If Loop For OP = L To R P(OP) = Q(OP) Next OP End If End Sub
Sub pInsertS(L As Long, R As Long, A() As String, P() As Long) Dim LP As Long Dim RP As Long Dim TMP As Long Dim T As String For RP = L + 1 To R TMP = P(RP) T = A(TMP) For LP = RP To L + 1 Step -1 If T < A(P(LP - 1)) Then P(LP) = P(LP - 1) Else Exit For Next LP P(LP) = TMP Next RP End Sub
Sub MergeSortL(L As Long, R As Long, A() As Long, B() As Long) Dim LP As Long Dim RP As Long Dim OP As Long Dim MID As Long If R - L < 12 Then InsertL L, R, A() Else MID = (L + R) \ 2 MergeSortL L, MID, A, B MergeSortL MID + 1, R, A, B LP = L RP = MID + 1 OP = L Do If A(LP) <= A(RP) Then B(OP) = A(LP) OP = OP + 1 LP = LP + 1 If LP > MID Then Do B(OP) = A(RP) OP = OP + 1 RP = RP + 1 Loop Until RP > R Exit Do End If Else B(OP) = A(RP) OP = OP + 1 RP = RP + 1 If RP > R Then Do B(OP) = A(LP) OP = OP + 1 LP = LP + 1 Loop Until LP > MID Exit Do End If End If Loop For OP = L To R A(OP) = B(OP) Next OP End If End Sub
Sub InsertL(L As Long, R As Long, A() As Long) Dim LP As Long Dim RP As Long Dim TMP As Long For RP = L + 1 To R TMP = A(RP) For LP = RP To L + 1 Step -1 If TMP < A(LP - 1) Then A(LP) = A(LP - 1) Else Exit For Next LP A(LP) = TMP Next RP End Sub |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|