|
|
|
Click here to copy the following block |
Sub pShuttleMergeSortS(LO As Long, HI As Long, A() As String, P() As Long, _ Q() As Long) Dim Length As Double Dim nRuns As Long Dim Stack() As Long Dim I As Long Dim L As Long Dim R As Long Dim LP As Long Dim RP As Long Dim OP As Long Dim TMP As String Dim Forward As Boolean Length = 1 + HI - LO nRuns = 1 While Length > 20 Length = Length / 4 nRuns = nRuns * 4 Wend
ReDim Stack(1 To nRuns) For I = 1 To nRuns - 1 Stack(I) = LO + (Length * CDbl(I)) Next I Stack(nRuns) = HI L = LO For I = 1 To nRuns R = Stack(I) For RP = L + 1 To R OP = P(RP) TMP = A(OP) For LP = RP To L + 1 Step -1 If TMP < A(P(LP - 1)) Then P(LP) = P(LP - 1) Else Exit For Next LP P(LP) = OP Next RP L = R + 1 Next I Forward = True While nRuns > 1 R = LO - 1 If Forward Then For I = 2 To nRuns Step 2 LP = R + 1 OP = LP L = Stack(I - 1) RP = L + 1 R = Stack(I) Do If A(P(LP)) <= A(P(RP)) Then Q(OP) = P(LP) OP = OP + 1 LP = LP + 1 If LP > L 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 > L Exit Do End If End If Loop Stack(I \ 2) = R Next I Else For I = 2 To nRuns Step 2 LP = R + 1 OP = LP L = Stack(I - 1) RP = L + 1 R = Stack(I) Do If A(Q(LP)) <= A(Q(RP)) Then P(OP) = Q(LP) OP = OP + 1 LP = LP + 1 If LP > L Then Do P(OP) = Q(RP) OP = OP + 1 RP = RP + 1 Loop Until RP > R Exit Do End If Else P(OP) = Q(RP) OP = OP + 1 RP = RP + 1 If RP > R Then Do P(OP) = Q(LP) OP = OP + 1 LP = LP + 1 Loop Until LP > L Exit Do End If End If Loop Stack(I \ 2) = R Next I End If nRuns = nRuns \ 2 Forward = Not Forward Wend End Sub
Sub ShuttleMergeSortL(LO As Long, HI As Long, A() As Long, B() As Long) Dim Length As Double Dim nRuns As Long Dim Stack() As Long Dim I As Long Dim L As Long Dim R As Long Dim LP As Long Dim RP As Long Dim OP As Long Dim TMP As String Dim Forward As Boolean Length = 1 + HI - LO nRuns = 1 While Length > 20 Length = Length / 4 nRuns = nRuns * 4 Wend
ReDim Stack(1 To nRuns) For I = 1 To nRuns - 1 Stack(I) = LO + (Length * CDbl(I)) Next I Stack(nRuns) = HI L = LO For I = 1 To nRuns R = Stack(I) 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 L = R + 1 Next I Forward = True While nRuns > 1 R = LO - 1 If Forward Then For I = 2 To nRuns Step 2 LP = R + 1 OP = LP L = Stack(I - 1) RP = L + 1 R = Stack(I) Do If A(LP) <= A(RP) Then B(OP) = A(LP) OP = OP + 1 LP = LP + 1 If LP > L 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 > L Exit Do End If End If Loop Stack(I \ 2) = R Next I Else For I = 2 To nRuns Step 2 LP = R + 1 OP = LP L = Stack(I - 1) RP = L + 1 R = Stack(I) Do If B(LP) <= B(RP) Then A(OP) = B(LP) OP = OP + 1 LP = LP + 1 If LP > L Then Do A(OP) = B(RP) OP = OP + 1 RP = RP + 1 Loop Until RP > R Exit Do End If Else A(OP) = B(RP) OP = OP + 1 RP = RP + 1 If RP > R Then Do A(OP) = B(LP) OP = OP + 1 LP = LP + 1 Loop Until LP > L Exit Do End If End If Loop Stack(I \ 2) = R Next I End If nRuns = nRuns \ 2 Forward = Not Forward Wend 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 ) |
|
|