VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Shell Sort for Strings and other data types

by Jolyon Bloomfield (9 Submissions)
Category: Math/Dates
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (4 Votes)

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

Rate Shell Sort for Strings and other data types

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

Download this snippet    Add to My Saved Code

Shell Sort for Strings and other data types Comments

No comments have been posted about Shell Sort for Strings and other data types. Why not be the first to post a comment about Shell Sort for Strings and other data types.

Post your comment

Subject:
Message:
0/1000 characters