Sorting is a basic but very important concept in programming. Whenever we want data to appear in a proper order, such as numbers from smallest to largest or names in alphabetical order, a sorting algorithm is needed. One simple and popular sorting technique is Selection Sort. The main idea behind Selection Sort is easy to understand. It repeatedly finds the smallest value from an unsorted part of the array and places it at the correct position.
with hands-on learning.
get the skills and confidence to land your next move.
Selection Sort is often taught to beginners because its logic is clear and predictable. Even though it is not the fastest sorting algorithm, it helps learners understand how arrays, loops, and comparisons work together. In VB .NET, implementing Selection Sort is a great way to practice structured thinking and basic programming skills that are useful in many real-world applications.
Program 1: Selection Sort Using Simple Nested Loops
This program demonstrates the most basic form of Selection Sort using nested loops. It sorts a predefined array in ascending order.
Module Module1
Sub Main()
Dim numbers() As Integer = {64, 25, 12, 22, 11}
Dim n As Integer = numbers.Length
Dim num As Integer
For i As Integer = 0 To n - 2
Dim minIndex As Integer = i
For j As Integer = i + 1 To n - 1
If numbers(j) < numbers(minIndex) Then
minIndex = j
End If
Next
Dim temp As Integer = numbers(minIndex)
numbers(minIndex) = numbers(i)
numbers(i) = temp
Next
Console.WriteLine("Sorted array:")
For Each num In numbers
Console.Write(num & " ")
Next
Console.ReadLine()
End Sub
End ModuleThis program works by selecting the smallest value in each pass and swapping it with the current position. The outer loop controls the position being filled, while the inner loop finds the smallest value. Beginners can clearly see how Selection Sort reduces the unsorted part of the array step by step.
Program 2: Selection Sort with Clear Output Display
This version prints the array before and after sorting to make the process easier to understand.
Module Module1
Sub Main()
Dim numbers() As Integer = {29, 10, 14, 37, 13}
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
Dim minIndex As Integer = i
For j As Integer = i + 1 To n - 1
If numbers(j) < numbers(minIndex) Then
minIndex = j
End If
Next
Dim temp As Integer = numbers(minIndex)
numbers(minIndex) = numbers(i)
numbers(i) = temp
Next
Console.WriteLine("Sorted array:")
For Each num In numbers
Console.Write(num & " ")
Next
Console.ReadLine()
End Sub
End ModuleBy displaying both the original and sorted arrays, this program helps beginners visually understand the effect of Selection Sort. It makes learning more interactive and builds confidence in reading program output.
Program 3: Selection Sort in Descending Order
This program modifies the logic slightly to sort numbers from largest to smallest.
Module Module1
Sub Main()
Dim numbers() As Integer = {5, 3, 8, 4, 2}
Dim n As Integer = numbers.Length
Dim num As Integer
For i As Integer = 0 To n - 2
Dim maxIndex As Integer = i
For j As Integer = i + 1 To n - 1
If numbers(j) > numbers(maxIndex) Then
maxIndex = j
End If
Next
Dim temp As Integer = numbers(maxIndex)
numbers(maxIndex) = numbers(i)
numbers(i) = temp
Next
Console.WriteLine("Sorted array in descending order:")
For Each num In numbers
Console.Write(num & " ")
Next
Console.ReadLine()
End Sub
End ModuleThe only difference here is the comparison condition, which now looks for the largest value instead of the smallest. This shows beginners how flexible Selection Sort can be with just a small change in logic.
Program 4: Selection Sort Using a Separate Function
This example places the sorting logic inside a function for better code organization.
Module Module1
Sub SelectionSort(ByRef arr() As Integer)
Dim n As Integer = arr.Length
For i As Integer = 0 To n - 2
Dim minIndex As Integer = i
For j As Integer = i + 1 To n - 1
If arr(j) < arr(minIndex) Then
minIndex = j
End If
Next
Dim temp As Integer = arr(minIndex)
arr(minIndex) = arr(i)
arr(i) = temp
Next
End Sub
Sub Main()
Dim numbers() As Integer = {20, 12, 10, 15, 2}
Dim num As Integer
SelectionSort(numbers)
Console.WriteLine("Sorted array:")
For Each num In numbers
Console.Write(num & " ")
Next
Console.ReadLine()
End Sub
End ModuleUsing a separate function makes the program cleaner and easier to reuse. Beginners learn how sorting logic can be written once and applied to different datasets.
Program 5: Selection Sort with Step-by-Step Style
This program focuses on clarity and beginner-friendly structure.
Module Module1
Sub Main()
Dim numbers() As Integer = {9, 7, 5, 3, 1}
Dim n As Integer = numbers.Length
Dim num As Integer
For i As Integer = 0 To n - 2
Dim minIndex As Integer = i
For j As Integer = i + 1 To n - 1
If numbers(j) < numbers(minIndex) Then
minIndex = j
End If
Next
Dim temp As Integer = numbers(minIndex)
numbers(minIndex) = numbers(i)
numbers(i) = temp
Next
Console.WriteLine("Final sorted array:")
For Each num In numbers
Console.Write(num & " ")
Next
Console.ReadLine()
End Sub
End ModuleThis version keeps everything simple and readable. It is ideal for beginners who want to focus on understanding how Selection Sort works without extra complexity.
Frequently Asked Questions (FAQ)
This section answers common beginner questions about Selection Sort in VB .NET.
Q1. What is Selection Sort mainly used for?
Selection Sort is mainly used for learning purposes and small datasets where simplicity matters more than speed.
Q2. Is Selection Sort better than Bubble Sort?
Selection Sort usually performs fewer swaps than Bubble Sort, but both have similar time complexity.
Q3. Can Selection Sort work with negative numbers?
Yes, Selection Sort works perfectly with negative numbers as long as comparisons are valid.
Q4. Is Selection Sort suitable for large datasets?
Selection Sort is not ideal for large datasets because it is slower than advanced algorithms.
Q5. Why should beginners learn Selection Sort?
It helps beginners understand array indexing, comparisons, and swapping in a very clear way.
Conclusion
Selection Sort is a simple and beginner-friendly sorting algorithm that is perfect for learning the basics of sorting in VB .NET. Its clear logic makes it easy to follow and understand, even for those who are new to programming. Through different program examples, we saw how Selection Sort can be written in multiple ways and adapted for different needs.
By practicing these VB .NET Selection Sort programs, beginners can strengthen their understanding of loops, arrays, and functions. Once these basics are clear, moving on to more advanced sorting algorithms becomes much easier and more enjoyable.




