Results 1 to 2 of 2

Thread: [VB.net] Modified Merge Sort

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2004
    Posts
    10

    [VB.net] Modified Merge Sort

    A sorting function I created - It is based on the Merge Sort algorithm.

    I know sorting functions are not really needed anymore and this one still needs a bit of refinement. Still I thought I might share it since I created it.

    VB Code:
    1. Public Sub splitsort1(ByRef pvarArray() As VariantType, Optional ByVal leftpos As Integer = 0, Optional ByVal rightpos As Integer = -2)
    2.         'filling initial array size - needed at start
    3.         If rightpos = -2 Then
    4.             rightpos = pvarArray.Length - 1
    5.         End If
    6.         'checking if recursion needs to end making sure both positions are not equal or right is greater than left
    7.         If rightpos <> leftpos And rightpos > leftpos Then
    8.             Dim rcount As Integer = 0
    9.             Dim temp As VariantType = VariantType.Null
    10.             Dim count As Integer = 0
    11.             Dim l1 As Integer = leftpos
    12.             'declaring midpoint
    13.             Dim mid As Integer = pvarArray((rightpos - leftpos + 1) \ 2 + leftpos)
    14.             Dim tvararray(pvarArray.Length - 1) As VariantType
    15.             tvararray = pvarArray.Clone()
    16.             'Looping to put elements into two groups around midpoint
    17.             For l As Integer = leftpos To rightpos
    18.                 If pvarArray(l) < mid Then
    19.                     'left pile
    20.                     tvararray(leftpos) = pvarArray(l)
    21.                     leftpos += 1
    22.                 Else
    23.                     If pvarArray(l) = mid Then
    24.                         'mid removed
    25.                         temp = pvarArray(l)
    26.                         count += 1
    27.                     Else
    28.                         'right pile
    29.                         tvararray(rightpos - rcount) = pvarArray(l)
    30.                         rcount += 1
    31.                     End If
    32.                 End If
    33.             Next
    34.             'filling midpoint/s in array - these are in their final position
    35.             For x = 1 To count
    36.                 tvararray(leftpos + x - 1) = temp
    37.             Next
    38.             pvarArray = tvararray.Clone()
    39.             'recursion left
    40.             splitsort1(pvarArray, 0 + l1, leftpos - 1)
    41.             'recursion right
    42.             splitsort1(pvarArray, leftpos + count, leftpos + rcount + count - 1)
    43.         End If
    44.     End Sub

  2. #2
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: [VB.net] Modified Merge Sort

    You shouldn't use VariantType, it can make some really bad code and reduce efficiency. Instead, use templating (Google it!). Also, for another small efficiency improvement, use AndAlso instead of And.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width