Results 1 to 40 of 100

Thread: VB6: Sorting algorithms (sort array, sorting arrays)

Threaded View

  1. #11

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Sorting algorithms (sort array, sorting arrays)

    Merge sort

    Stable: Yes
    In-Place: No
    Online: No
    Recursive: Yes
    Grade: A-

    Similar to quicksort, merge sort is a recursive algorithm based on a divide and conquer strategy. First, the sequence to be sorted is split into two halves. Each half is then sorted independently, and the two sorted halves are merged to a sorted sequence. Merge sort takes advantage of the ease of merging together already sorted lists.

    In many implementations, merge sort calls out to an external algorithm -- usually insertion sort -- when it reaches a level of around 10-20 elements. This is not necessary in Visual Basic; in fact such an implementation appears to slow merge sort's overall performance in practice.

    Instead, the implementation here uses the purest form of merge sort, where the list is recursively divided into halves until it reaches a list size of two, at which point those two elements are sorted.

    Unfortunately, extra memory is required to combine two sorted lists together. The extra memory in this implementation is in the form of a full copy of the initial array. There are various optimizations that can be made to improve on this, but that is left as an exercise for the reader.

    Invented in 1945 by John von Neumann, merge sort is far and away the fastest stable algorithm.
    vb Code:
    1. ' Omit pvarMirror, plngLeft & plngRight; they are used internally during recursion
    2. Public Sub MergeSort1(ByRef pvarArray As Variant, Optional pvarMirror As Variant, Optional ByVal plngLeft As Long, Optional ByVal plngRight As Long)
    3.     Dim lngMid As Long
    4.     Dim L As Long
    5.     Dim R As Long
    6.     Dim O As Long
    7.     Dim varSwap As Variant
    8.  
    9.     If plngRight = 0 Then
    10.         plngLeft = LBound(pvarArray)
    11.         plngRight = UBound(pvarArray)
    12.         ReDim pvarMirror(plngLeft To plngRight)
    13.     End If
    14.     lngMid = plngRight - plngLeft
    15.     Select Case lngMid
    16.         Case 0
    17.         Case 1
    18.             If pvarArray(plngLeft) > pvarArray(plngRight) Then
    19.                 varSwap = pvarArray(plngLeft)
    20.                 pvarArray(plngLeft) = pvarArray(plngRight)
    21.                 pvarArray(plngRight) = varSwap
    22.             End If
    23.         Case Else
    24.             lngMid = lngMid \ 2 + plngLeft
    25.             MergeSort1 pvarArray, pvarMirror, plngLeft, lngMid
    26.             MergeSort1 pvarArray, pvarMirror, lngMid + 1, plngRight
    27.             ' Merge the resulting halves
    28.             L = plngLeft ' start of first (left) half
    29.             R = lngMid + 1 ' start of second (right) half
    30.             O = plngLeft ' start of output (mirror array)
    31.             Do
    32.                 If pvarArray(R) < pvarArray(L) Then
    33.                     pvarMirror(O) = pvarArray(R)
    34.                     R = R + 1
    35.                     If R > plngRight Then
    36.                         For L = L To lngMid
    37.                             O = O + 1
    38.                             pvarMirror(O) = pvarArray(L)
    39.                         Next
    40.                         Exit Do
    41.                     End If
    42.                 Else
    43.                     pvarMirror(O) = pvarArray(L)
    44.                     L = L + 1
    45.                     If L > lngMid Then
    46.                         For R = R To plngRight
    47.                             O = O + 1
    48.                             pvarMirror(O) = pvarArray(R)
    49.                         Next
    50.                         Exit Do
    51.                     End If
    52.                 End If
    53.                 O = O + 1
    54.             Loop
    55.             For O = plngLeft To plngRight
    56.                 pvarArray(O) = pvarMirror(O)
    57.             Next
    58.     End Select
    59. End Sub
    Last edited by Ellis Dee; May 26th, 2008 at 04:01 PM.

Tags for this Thread

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