VB .NET Program to Implement Insertion Sort

VB .NET Program to Implement Insertion Sort

Sorting is one of the most common tasks in programming, and it plays a big role in making data easier to read, search, and manage. When numbers or values are sorted, programs often run faster and results become clearer. One simple and beginner-friendly sorting technique is Insertion Sort. This algorithm works in a way that feels very natural, similar to how people sort playing cards in their hands by picking one card at a time and placing it in the correct position.

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

Insertion Sort is important because it is easy to understand and implement, especially for new learners. While it is not the fastest sorting algorithm for very large datasets, it performs well on small lists and nearly sorted data. In VB .NET, learning Insertion Sort helps beginners understand loops, array indexing, comparisons, and shifting elements, which are all essential programming skills.

Program 1: Insertion Sort Using Basic Loops

This program shows the simplest form of Insertion Sort using loops. It sorts a predefined array of integers in ascending order.

Module Module1

    Sub Main()

        Dim numbers() As Integer = {12, 11, 13, 5, 6}
        Dim n As Integer = numbers.Length
        Dim num As Integer

        For i As Integer = 1 To n - 1

            Dim key As Integer = numbers(i)
            Dim j As Integer = i - 1

            While j >= 0 AndAlso numbers(j) > key

                numbers(j + 1) = numbers(j)
                j -= 1

            End While

            numbers(j + 1) = key

        Next

        Console.WriteLine("Sorted array:")

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

        Console.ReadLine()

    End Sub

End Module

This program works by taking one element at a time and placing it in the correct position among the already sorted part of the array. Beginners can easily follow how values are shifted to make space for the current element. It is useful because it clearly shows the core idea behind Insertion Sort.

Program 2: Insertion Sort with Original and Sorted Output

This version displays the array before and after sorting to help beginners see the change clearly.

Module Module1

    Sub Main()

        Dim numbers() As Integer = {9, 3, 1, 5, 4}
        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 = 1 To n - 1

            Dim key As Integer = numbers(i)
            Dim j As Integer = i - 1

            While j >= 0 AndAlso numbers(j) > key

                numbers(j + 1) = numbers(j)
                j -= 1

            End While

            numbers(j + 1) = key

        Next

        Console.WriteLine("Sorted array:")

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

        Console.ReadLine()

    End Sub

End Module

By printing the array before and after sorting, this program helps beginners understand the effect of the algorithm. It makes learning more visual and easier to remember.

Program 3: Insertion Sort in Descending Order

This program modifies the comparison to sort numbers from largest to smallest.

Module Module1

    Sub Main()

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

        For i As Integer = 1 To n - 1

            Dim key As Integer = numbers(i)
            Dim j As Integer = i - 1

            While j >= 0 AndAlso numbers(j) < key

                numbers(j + 1) = numbers(j)
                j -= 1

            End While

            numbers(j + 1) = key

        Next

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

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

        Console.ReadLine()

    End Sub

End Module

The only change here is the comparison sign, which now looks for larger values first. This example shows beginners how small changes in logic can produce different sorting results.

Program 4: Insertion Sort Using a Separate Function

This example places the sorting logic inside a function for better structure and reuse.

Module Module1

    Sub InsertionSort(ByRef arr() As Integer)

        For i As Integer = 1 To arr.Length - 1

            Dim key As Integer = arr(i)
            Dim j As Integer = i - 1

            While j >= 0 AndAlso arr(j) > key

                arr(j + 1) = arr(j)
                j -= 1

            End While

            arr(j + 1) = key

        Next

    End Sub

    Sub Main()

        Dim numbers() As Integer = {15, 10, 8, 12, 6}
        Dim num As Integer

        InsertionSort(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 cleaner and easier to maintain. Beginners learn how to separate logic and reuse code, which is a valuable programming habit.

Program 5: Insertion Sort with Negative Numbers

This program shows that Insertion Sort can also handle negative values correctly.

Module Module1

    Sub Main()

        Dim numbers() As Integer = {3, -1, 4, -5, 2}
        Dim n As Integer = numbers.Length
        Dim num As Integer

        For i As Integer = 1 To n - 1

            Dim key As Integer = numbers(i)
            Dim j As Integer = i - 1

            While j >= 0 AndAlso numbers(j) > key

                numbers(j + 1) = numbers(j)
                j -= 1

            End While

            numbers(j + 1) = key

        Next

        Console.WriteLine("Sorted array with negative numbers:")

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

        Console.ReadLine()

    End Sub

End Module

This program proves that Insertion Sort works for both positive and negative numbers. It reassures beginners that the algorithm is reliable for different kinds of data.

Frequently Asked Questions (FAQ)

This section answers common questions beginners often have about Insertion Sort in VB .NET.

Q1. What is Insertion Sort best used for?
Insertion Sort works best with small datasets or lists that are already partially sorted.

Q2. Is Insertion Sort faster than Selection Sort?
Insertion Sort is usually faster than Selection Sort for small or nearly sorted data.

Q3. Can Insertion Sort handle negative numbers?
Yes, Insertion Sort can sort both negative and positive numbers correctly.

Q4. Is Insertion Sort suitable for large datasets?
Insertion Sort is not ideal for very large datasets because it is slower compared to advanced algorithms.

Q5. Why should beginners learn Insertion Sort?
It teaches important concepts like shifting elements, comparisons, and loop control in a simple way.

Conclusion

Insertion Sort is a simple and powerful sorting algorithm that is perfect for beginners learning VB .NET. Its step-by-step approach makes it easy to understand how elements are compared and placed in the correct order. Through different program examples, we saw how Insertion Sort can be implemented in various ways to handle different situations.

By practicing these VB .NET Insertion Sort programs, beginners can build a strong foundation in sorting logic and array handling. With regular practice, moving on to more advanced sorting techniques will feel much easier and more confident.

Scroll to Top