VBA Bubble Sort : Array Sort « Data Type « VBA / Excel / Access / Word






VBA Bubble Sort

 
Public Sub BubbleSort()
    Dim tempVar As Integer
    Dim anotherIteration As Boolean
    Dim I As Integer
    Dim iterationNum As Integer
    Dim sortArray(10) As Integer

    For I = 2 To 11
        sortArray(I - 2) = Cells(I, "A").Value
    Next I
    Do
        anotherIteration = False
        For I = 0 To 8
            If sortArray(I) > sortArray(I + 1) Then
                tempVar = sortArray(I)
                sortArray(I) = sortArray(I + 1)
                sortArray(I + 1) = tempVar
                anotherIteration = True
            End If
        Next I
        iterationNum = iterationNum + 1
        Cells(1, iterationNum + 2).Value = iterationNum
        For I = 0 To 9
            Cells(I + 2, iterationNum + 2).Value = sortArray(I)
        Next I
    Loop While anotherIteration = True
End Sub

 








Related examples in the same category

1.using dynamic arrays in bubble sort
2.Performing a Binary Search through an Array
3.Quick sort
4.Quick Sort 2