|
-
Jun 12th, 2007, 07:10 PM
#11
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:
' Omit pvarMirror, plngLeft & plngRight; they are used internally during recursion
Public Sub MergeSort1(ByRef pvarArray As Variant, Optional pvarMirror As Variant, Optional ByVal plngLeft As Long, Optional ByVal plngRight As Long)
Dim lngMid As Long
Dim L As Long
Dim R As Long
Dim O As Long
Dim varSwap As Variant
If plngRight = 0 Then
plngLeft = LBound(pvarArray)
plngRight = UBound(pvarArray)
ReDim pvarMirror(plngLeft To plngRight)
End If
lngMid = plngRight - plngLeft
Select Case lngMid
Case 0
Case 1
If pvarArray(plngLeft) > pvarArray(plngRight) Then
varSwap = pvarArray(plngLeft)
pvarArray(plngLeft) = pvarArray(plngRight)
pvarArray(plngRight) = varSwap
End If
Case Else
lngMid = lngMid \ 2 + plngLeft
MergeSort1 pvarArray, pvarMirror, plngLeft, lngMid
MergeSort1 pvarArray, pvarMirror, lngMid + 1, plngRight
' Merge the resulting halves
L = plngLeft ' start of first (left) half
R = lngMid + 1 ' start of second (right) half
O = plngLeft ' start of output (mirror array)
Do
If pvarArray(R) < pvarArray(L) Then
pvarMirror(O) = pvarArray(R)
R = R + 1
If R > plngRight Then
For L = L To lngMid
O = O + 1
pvarMirror(O) = pvarArray(L)
Next
Exit Do
End If
Else
pvarMirror(O) = pvarArray(L)
L = L + 1
If L > lngMid Then
For R = R To plngRight
O = O + 1
pvarMirror(O) = pvarArray(R)
Next
Exit Do
End If
End If
O = O + 1
Loop
For O = plngLeft To plngRight
pvarArray(O) = pvarMirror(O)
Next
End Select
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|