VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Two sorts and one method to reverse an array

by Anonymous (267 Submissions)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Originally Published: Sat 5th July 2003
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Two sorts and one method to reverse an array

Rate Two sorts and one method to reverse an array




Public Sub bubbleSort(Arr(), Optional sortAscending As Boolean = True)

    Dim x As Long, y As Long, vy, vx, temp
    Dim lo As Long, up As Long, incr As Integer

    If sortAscending Then
        lo = LBound(Arr): up = UBound(Arr): incr = 1
    Else
        lo = UBound(Arr): up = LBound(Arr): incr = -1
    End If

    For y = lo To up Step incr
        For x = y + incr To up Step incr
            vx = Arr(x): vy = Arr(y)
            If vx < vy Then
                Arr(y) = vx
                Arr(x) = vy
            End If
        Next x
    Next y

End Sub

Public Sub bubbleSortText(Arr(), Optional sortAscending As Boolean = True)

    Dim x As Long, y As Long, vy, vx, temp
    Dim lo As Long, up As Long, incr As Integer

    If sortAscending Then
        lo = LBound(Arr): up = UBound(Arr): incr = 1
    Else
        lo = UBound(Arr): up = LBound(Arr): incr = -1
    End If

    For y = lo To up Step incr
        For x = y + incr To up Step incr
            vx = Arr(x): vy = Arr(y)
            If StrComp(vx, vy, vbTextCompare) = -1 Then
                Arr(y) = vx
                Arr(x) = vy
            End If
        Next x
    Next y

End Sub

Public Sub arrayReverse(Arr())

    Dim tmp
    Dim i As Long, j As Long, lower As Long, upper As Long, mx As Long

    lower = LBound(Arr)
    upper = UBound(Arr)

    If (upper <> lower) Then

        If (upper Mod 2 = 0) Then mx = upper / 2 - 1 Else mx = upper \ 2

        For i = lower To mx
            j = upper - i
            tmp = Arr(j)
            Arr(j) = Arr(i)
            Arr(i) = tmp
        Next

    End If

End Sub

Download this snippet    Add to My Saved Code

Two sorts and one method to reverse an array Comments

No comments have been posted about Two sorts and one method to reverse an array. Why not be the first to post a comment about Two sorts and one method to reverse an array.

Post your comment

Subject:
Message:
0/1000 characters