Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Sorting and Searching

 

Link to C++ Section of Manual (extended version)

Visual Basic Bubble Sort Algorithm

Output

Exercises

 

 

Visual Basic Bubble Sort Algorithm

Module Module1

 

    Sub Main()

        Dim randomizer As New Random

        Dim myArray(8) As Integer    '8 is the index or subscript of the last element

        For i As Integer = 0 To myArray.Length - 1

            myArray(i) = randomizer.Next(1, 10)

        Next

        Console.WriteLine("The array before sorting")

        For j As Integer = 0 To myArray.Length - 1

            Console.Write(myArray(j) & " ")

        Next

        Console.WriteLine()

 

 

        For k As Integer = 1 To myArray.Length - 1

            For m As Integer = 0 To myArray.Length - k - 1

                If (myArray(m) > myArray(m + 1)) Then

                    Dim temp As Integer = myArray(m)

                    myArray(m) = myArray(m + 1)

                    myArray(m + 1) = temp

                End If

            Next

        Next

 

        Console.WriteLine("The array after Sorting")

        For n As Integer = 0 To myArray.Length - 1

            Console.Write(myArray(n) & " ")

        Next

 

    End Sub

End Module

 

 

Output

 

 


 

Exercises

Exercise 1

Create a one-dimensional array of 20 random integers in the range 1 to 9

Print the array, 10 elements per line and label it The Array Before Sorting

Sort the array using bubble sort

Print the array, 10 elements per line and label it The Array After Sorting