VB .NET Program to Implement Bubble Sort

VB .NET Program to Implement Bubble Sort

Sorting is one of the first and most important topics in programming. Whenever data needs to be arranged in order, such as numbers, names, or scores, a sorting algorithm is used. One of the simplest and most beginner-friendly sorting techniques is Bubble Sort. It works by repeatedly comparing nearby values and swapping them if they are in the wrong order. Over time, the largest values slowly move to the end, just like bubbles rising to the surface of water.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Bubble Sort may not be the fastest algorithm, but it is very easy to understand. Because of this, it is often used in classrooms and beginner tutorials. In VB .NET, Bubble Sort helps new programmers learn loops, conditions, and array handling in a clear and practical way. Understanding Bubble Sort also makes it easier to learn more advanced sorting algorithms later.

Program 1: Bubble Sort Using Simple Nested Loops

This program shows the most basic Bubble Sort implementation using two loops. It sorts an array of numbers in ascending order.

Module Module1

    Sub Main()

        Dim numbers() As Integer = {64, 34, 25, 12, 22, 11, 90}
        Dim n As Integer = numbers.Length
        Dim num As Integer

        For i As Integer = 0 To n - 2

            For j As Integer = 0 To n - i - 2

                If numbers(j) > numbers(j + 1) Then

                    Dim temp As Integer = numbers(j)
                    numbers(j) = numbers(j + 1)
                    numbers(j + 1) = temp

                End If

            Next

        Next

        Console.WriteLine("Sorted array:")

        For Each num In numbers
            Console.Write(num & " ")
        Next

        Console.ReadLine()

    End Sub

End Module

This program compares two adjacent numbers and swaps them if needed. With each outer loop pass, the largest value moves to the end of the array. Beginners can clearly see how loops and conditions work together to perform sorting.

Program 2: Bubble Sort Using a Flag for Optimization

This version improves Bubble Sort by stopping early if the array is already sorted.

Module Module1

    Sub Main()

        Dim numbers() As Integer = {5, 1, 4, 2, 8}
        Dim n As Integer = numbers.Length
        Dim swapped As Boolean
        Dim num As Integer

        For i As Integer = 0 To n - 2

            swapped = False

            For j As Integer = 0 To n - i - 2

                If numbers(j) > numbers(j + 1) Then

                    Dim temp As Integer = numbers(j)
                    numbers(j) = numbers(j + 1)
                    numbers(j + 1) = temp
                    swapped = True

                End If

            Next

            If Not swapped Then Exit For

        Next

        Console.WriteLine("Sorted array:")

        For Each num In numbers
            Console.Write(num & " ")
        Next

        Console.ReadLine()

    End Sub

End Module

The swapped flag checks if any swap happened during a pass. If no swaps occur, the array is already sorted and the loop stops early. This makes the algorithm faster in some cases and helps beginners understand performance improvements.

Program 3: Bubble Sort in Descending Order

This program sorts numbers from largest to smallest instead of ascending order.

Module Module1

    Sub Main()

        Dim numbers() As Integer = {3, 7, 2, 9, 1}
        Dim n As Integer = numbers.Length
        Dim num As Integer

        For i As Integer = 0 To n - 2

            For j As Integer = 0 To n - i - 2

                If numbers(j) < numbers(j + 1) Then

                    Dim temp As Integer = numbers(j)
                    numbers(j) = numbers(j + 1)
                    numbers(j + 1) = temp

                End If

            Next

        Next

        Console.WriteLine("Sorted array in descending order:")

        For Each num In numbers
            Console.Write(num & " ")
        Next

        Console.ReadLine()

    End Sub

End Module

The logic is almost the same as ascending Bubble Sort, but the comparison sign is reversed. This helps beginners understand how small logic changes can completely change program behavior.

Program 4: Bubble Sort Using a Separate Function

This example places the sorting logic inside a function to make the code cleaner.

Module Module1

    Sub BubbleSort(ByRef arr() As Integer)

        Dim n As Integer = arr.Length

        For i As Integer = 0 To n - 2

            For j As Integer = 0 To n - i - 2

                If arr(j) > arr(j + 1) Then

                    Dim temp As Integer = arr(j)
                    arr(j) = arr(j + 1)
                    arr(j + 1) = temp

                End If

            Next

        Next

    End Sub

    Sub Main()

        Dim numbers() As Integer = {10, 7, 8, 9, 1, 5}
        Dim num As Integer

        BubbleSort(numbers)

        Console.WriteLine("Sorted array:")

        For Each num In numbers
            Console.Write(num & " ")
        Next

        Console.ReadLine()

    End Sub

End Module

Using a function makes the program easier to reuse and understand. Beginners learn how sorting logic can be separated from input and output, which is a good programming habit.

Program 5: Bubble Sort with User-Friendly Output

This program adds clear messages to help beginners follow the process.

Module Module1

    Sub Main()

        Dim numbers() As Integer = {6, 5, 3, 1, 8}
        Dim n As Integer = numbers.Length
        Dim num As Integer

        Console.WriteLine("Original array:")

        For Each num In numbers
            Console.Write(num & " ")
        Next

        Console.WriteLine()

        For i As Integer = 0 To n - 2

            For j As Integer = 0 To n - i - 2

                If numbers(j) > numbers(j + 1) Then

                    Dim temp As Integer = numbers(j)
                    numbers(j) = numbers(j + 1)
                    numbers(j + 1) = temp

                End If

            Next

        Next

        Console.WriteLine("Sorted array:")

        For Each num In numbers
            Console.Write(num & " ")
        Next

        Console.ReadLine()

    End Sub

End Module

This version focuses on clarity and readability. Beginners can easily compare the original and sorted arrays and understand what Bubble Sort does step by step.

Frequently Asked Questions (FAQ)

This section answers common beginner questions about Bubble Sort in VB .NET.

Q1. What is Bubble Sort used for?
Bubble Sort is mainly used for learning and small datasets where simplicity is more important than speed.

Q2. Is Bubble Sort fast?
Bubble Sort is slow for large datasets, but it is very easy to understand and implement.

Q3. Can Bubble Sort handle negative numbers?
Yes, Bubble Sort works with negative numbers as long as comparisons are valid.

Q4. Why is Bubble Sort important for beginners?
It teaches loops, conditions, and swapping logic in a very clear way.

Q5. Should Bubble Sort be used in real projects?
For large data, faster algorithms are better, but Bubble Sort is fine for learning and small tasks.

Conclusion

Bubble Sort is one of the best starting points for learning sorting algorithms in VB .NET. It is simple, readable, and perfect for understanding how sorting works behind the scenes. Through different programs, we saw how Bubble Sort can be written in various ways and improved step by step.

By practicing these examples, beginners can build strong confidence with arrays, loops, and logic. Once Bubble Sort is clear, learning advanced sorting techniques becomes much easier and more enjoyable.

Scroll to Top