VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Three numeric array sorts including Bubble sort, Selection sort, and Shell sort.

by VBPro (1 Submission)
Category: Math/Dates
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Thu 13th May 1999
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Three numeric array sorts including Bubble sort, Selection sort, and Shell sort.

Rate Three numeric array sorts including Bubble sort, Selection sort, and Shell sort.




'create an array of data
Dim lMyArray(0 To 9) As Long
Dim vTemp1 As Variant
Dim vTemp2 As Variant
Dim vTemp3 As Variant
Dim iLoop As Integer

    Randomize
    For iLoop = LBound(lMyArray) To UBound(lMyArray)
        lMyArray(iLoop) = Int(Rnd * 100) + 1
    Next iLoop
    vTemp1 = lMyArray
    vTemp2 = lMyArray
    vTemp3 = lMyArray
    
    Call BubbleSortNumbers(vTemp1)
    Call SelectionSortNumbers(vTemp2)
    Call ShellSortNumbers(vTemp3)
    
End Sub

Sub BubbleSortNumbers(iArray As Variant)

Dim lLoop1 As Long
Dim lLoop2 As Long
Dim lTemp As Long
    For lLoop1 = UBound(iArray) To LBound(iArray) Step -1
        For lLoop2 = LBound(iArray) + 1 To lLoop1
            If iArray(lLoop2 - 1) > iArray(lLoop2) Then
                lTemp = iArray(lLoop2 - 1)
                iArray(lLoop2 - 1) = iArray(lLoop2)
                iArray(lLoop2) = lTemp
            End If
        Next lLoop2
    Next lLoop1
End Sub


Sub SelectionSortNumbers(vArray As Variant)

Dim lLoop1 As Long
Dim lLoop2 As Long
Dim lMin As Long
Dim lTemp As Long
    For lLoop1 = LBound(vArray) To UBound(vArray) - 1
        lMin = lLoop1
        For lLoop2 = lLoop1 + 1 To UBound(vArray)
            If vArray(lLoop2) < vArray(lMin) Then lMin = lLoop2
        Next lLoop2
        lTemp = vArray(lMin)
        vArray(lMin) = vArray(lLoop1)
        vArray(lLoop1) = lTemp
    Next lLoop1
End Sub


Sub ShellSortNumbers(vArray As Variant)

Dim lLoop1 As Long
Dim lHold As Long
Dim lHValue As Long
Dim lTemp As Long

    lHValue = LBound(vArray)
    Do
        lHValue = 3 * lHValue + 1
    Loop Until lHValue > UBound(vArray)
    Do
        lHValue = lHValue / 3
        For lLoop1 = lHValue + LBound(vArray) To UBound(vArray)
            lTemp = vArray(lLoop1)
            lHold = lLoop1
            Do While vArray(lHold - lHValue) > lTemp
                vArray(lHold) = vArray(lHold - lHValue)
                lHold = lHold - lHValue
                If lHold < lHValue Then Exit Do
            Loop
            vArray(lHold) = lTemp
        Next lLoop1
    Loop Until lHValue = LBound(vArray)
End Sub

Download this snippet    Add to My Saved Code

Three numeric array sorts including Bubble sort, Selection sort, and Shell sort. Comments

No comments have been posted about Three numeric array sorts including Bubble sort, Selection sort, and Shell sort.. Why not be the first to post a comment about Three numeric array sorts including Bubble sort, Selection sort, and Shell sort..

Post your comment

Subject:
Message:
0/1000 characters