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)

    Shaker sort

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

    Shaker sort is a gap-based bubble sort with a twist. Most gap sorts -- shell sort, comb sort, et al. -- begin with a large gap and gradually shrink it down to one. By the time the gap reaches one, the list should be mostly ordered so the final pass should be efficient.

    Like other gap sorts, shaker sort begins with a large gap which gradually shrinks. However, once the gap reaches one, the gap gets expanded again before shrinking back toward one. The expanding and contracting gap sizes constitute the "shaking" part of shaker sort. Each additional expansion is smaller and smaller until it eventually resolves to one, when no further expansion is done. At this point the list is almost certain to be nearly sorted, so the final bubble sort pass is very efficient.
    vb Code:
    1. Public Function ShakerSort1(ByRef pvarArray As Variant)
    2.     Dim i As Long
    3.     Dim j As Long
    4.     Dim k As Long
    5.     Dim iMin As Long
    6.     Dim iMax As Long
    7.     Dim varSwap As Variant
    8.     Dim blnSwapped As Boolean
    9.    
    10.     iMin = LBound(pvarArray)
    11.     iMax = UBound(pvarArray)
    12.     i = (iMax - iMin) \ 2 + iMin
    13.     Do While i > iMin
    14.         j = i
    15.         Do While j > iMin
    16.             For k = iMin To i - j
    17.                 If pvarArray(k) > pvarArray(k + j) Then
    18.                     varSwap = pvarArray(k)
    19.                     pvarArray(k) = pvarArray(k + j)
    20.                     pvarArray(k + j) = varSwap
    21.                 End If
    22.             Next
    23.             j = j \ 2
    24.         Loop
    25.         i = i \ 2
    26.     Loop
    27.     iMax = iMax - 1
    28.     Do
    29.         blnSwapped = False
    30.         For i = iMin To iMax
    31.             If pvarArray(i) > pvarArray(i + 1) Then
    32.                 varSwap = pvarArray(i)
    33.                 pvarArray(i) = pvarArray(i + 1)
    34.                 pvarArray(i + 1) = varSwap
    35.                 blnSwapped = True
    36.             End If
    37.         Next i
    38.         If blnSwapped Then
    39.             blnSwapped = False
    40.             iMax = iMax - 1
    41.             For i = iMax To iMin Step -1
    42.                 If pvarArray(i) > pvarArray(i + 1) Then
    43.                     varSwap = pvarArray(i)
    44.                     pvarArray(i) = pvarArray(i + 1)
    45.                     pvarArray(i + 1) = varSwap
    46.                     blnSwapped = True
    47.                 End If
    48.             Next i
    49.             iMin = iMin + 1
    50.         End If
    51.     Loop Until Not blnSwapped
    52. End Function
    Last edited by Ellis Dee; May 20th, 2008 at 12:16 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