Search Tools Links Login

Shell Sort for Strings and other data types


Visual Basic 6, or VB Classic

Shell sort routine, created for strings, but easily changed for any data type. Pass in an array. Features arguments for the last element of the array to be sotred, and also the first. Fast sorting routine, should be compatible with all versions of VB, because it is straight maths, although the "optional" keyword in the declaration isn't compatible with earlier versions. Enjoy

Original Author: Jolyon Bloomfield

Code

Public Sub Sort(ByRef SortArray() As String, ByVal MaxRow As Integer, Optional ByVal MinRow As Integer = 1)
' Does a shell sort - fairly fast, and flexible
' In this case, sorts strings, but can easily be modified
' To suit other data types - simply change the definition of SortArray()
' and the next line, to the data type of your choice.
Dim TempSwap As String
Dim Offset As Integer
Dim Switch As Integer
Dim Limit As Integer
Dim Row As Integer
' Set comparison offset to half the number of records in SortArray:
Offset = (MaxRow - MinRow + 1) 2
Do While Offset > 0     ' Loop until offset gets to zero.
Limit = MaxRow - Offset
Do
  Switch = 0     ' Assume no switches at this offset.
  ' Compare elements and switch ones out of order:
  For Row = MinRow To Limit
   If UCase(SortArray(Row)) > UCase(SortArray(Row + Offset)) = True Then
    TempSwap = SortArray(Row)
    SortArray(Row) = SortArray(Row + Offset)
    SortArray(Row + Offset) = TempSwap
    Switch = Row
   End If
  Next Row
  ' Sort on next pass only to where last switch was made:
  Limit = Switch - Offset
Loop While Switch
' No switches at last offset, try one half as big:
Offset = Offset 2
Loop
End Sub

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 105 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.