Approx. read time: 4.6 min.
Post: Computer Programming Sorting Algorithms
Sorting Algorithms with VB.NET: Bubble Sort
Let’s get straight to business. The choice of sorting algorithm depends on the application, purpose, and data. These variables will determine which sorting algorithm to use.
For simplicity, we’ll use VB.NET in Microsoft Visual Studio, which is available for free at Microsoft.com.
Key Concept: Application, Purpose, & Data → Sorting Algorithm
What are we doing?
Examples:
- Spy agency using the Internet for surveillance/communication.
- VoIP & Telecommunications sorting network signals to filter out pirates.
- Encryption and Decryption tasks.
- Sorting an array of numbers using the Bubble Sort algorithm.
For simplicity, let’s go with the last example.
Task: Sort an Array of Numbers Using the Bubble Sort Algorithm
Imagine an array of numbers from a lottery ticket (e.g., Lotto Max: 7 main numbers plus a bonus number). We will sort only the first 7 randomly generated numbers, while the last number remains unsorted.
Example Array:
Dim arr() As Integer = New Integer() {39, 24, 49, 9, 10, 16, 23}
What is the Bubble Sort Algorithm?
Definition: Bubble Sort is the simplest sorting algorithm. It repeatedly swaps adjacent elements if they are in the wrong order. The process is repeated until the list is sorted.
How It Works:
- Bubble Sort steps through the list, compares adjacent pairs, and swaps them if needed.
- The algorithm is named for the way elements “bubble” to the top of the list.
- Although simple, it’s slow and impractical for large datasets. However, it’s useful for small or nearly sorted arrays.
Example Walkthrough:
First Pass:
- (5 1 4 2 8) → (1 5 4 2 8)
- (1 5 4 2 8) → (1 4 5 2 8)
- (1 4 5 2 8) → (1 4 2 5 8)
- (1 4 2 5 8) → (1 4 2 5 8) [No swap needed]
Second Pass:
- (1 4 2 5 8) → (1 4 2 5 8)
- (1 4 2 5 8) → (1 2 4 5 8)
- (1 2 4 5 8) → (1 2 4 5 8)
- (1 2 4 5 8) → (1 2 4 5 8)
Third Pass:
- (1 2 4 5 8) → (1 2 4 5 8)
- (1 2 4 5 8) → (1 2 4 5 8)
- (1 2 4 5 8) → (1 2 4 5 8)
- (1 2 4 5 8) → (1 2 4 5 8)
Time Complexity:
- Worst and Average Case: O(n²)
- Best Case: O(n) when the array is already sorted.
- Auxiliary Space: O(1)
- Sorting In Place: Yes
- Stable: Yes
Why Use Bubble Sort?
Despite its simplicity and inefficiency for large datasets, Bubble Sort is practical for small sets of numbers, making it a good choice for our example.
Implementing Bubble Sort in VB.NET
Before we start, download and install Visual Studio Community Edition from Microsoft.com.
If you prefer not to install Visual Studio, you can use an online compiler like JDoodle. Note that online compilers only support console applications, not Windows Forms.
VB.NET Code for Bubble Sort:
Feel free to change the numbers in the array and see how the algorithm sorts them in less than a second.
Visual Studio Output
If you use Microsoft Visual Studio, your output should match the sorted array results.
Line-by-Line Explanation:
Module Declaration
Module Module1
- Explanation: Declares a module named
Module1
. In VB.NET, a module is a container for subroutines and functions.
Main Subroutine
Sub Main()
- Explanation: Defines the
Main
subroutine, which is the entry point of the program.
Array Declaration
' List of random numbers
Dim arr() As Integer = New Integer() {39, 24, 49, 9, 10, 16, 23}
- Explanation: Declares and initializes an array named
arr
with random numbers.
BubbleSort Call
' Sort the array using bubble sort
BubbleSort(arr, arr.Length)
- Explanation: Calls the
BubbleSort
subroutine, passing the arrayarr
and its length as arguments to sort the array.
Output the Sorted Array
' Output the sorted array
For Each num As Integer In arr
Console.WriteLine(num)
Next
- Explanation: Iterates through the sorted array and prints each element to the console.
Wait for Key Press
Console.ReadLine() ' Wait for keypress
- Explanation: Waits for the user to press a key before closing the console window. This allows the user to see the output.
End of Main Subroutine
End Sub
- Explanation: Marks the end of the
Main
subroutine.
BubbleSort Subroutine
Sub BubbleSort(ByVal dataset() As Integer, ByVal n As Integer)
- Explanation: Defines the
BubbleSort
subroutine, which takes an arraydataset
and its lengthn
as parameters.
Outer Loop
For i As Integer = 0 To n - 1
- Explanation: Starts the outer loop, which runs from
0
ton-1
. This loop controls the number of passes through the array.
Inner Loop
For j As Integer = n - 1 To i + 1 Step -1
- Explanation: Starts the inner loop, which runs from
n-1
toi+1
in reverse order. This loop compares and swaps adjacent elements if needed.
Element Comparison and Swap
If dataset(j) < dataset(j - 1) Then
' Swap elements
Dim temp As Integer = dataset(j)
dataset(j) = dataset(j - 1)
dataset(j - 1) = temp
End If
- Explanation: Compares the current element
dataset(j)
with the previous elementdataset(j-1)
. If the current element is smaller, it swaps them using a temporary variabletemp
.
End of Inner Loop
Next
- Explanation: Marks the end of the inner loop.
End of Outer Loop
Next
- Explanation: Marks the end of the outer loop.
End of BubbleSort Subroutine
End Sub
- Explanation: Marks the end of the
BubbleSort
subroutine.
End of Module
End Module
- Explanation: Marks the end of the
Module1
module.
Summary
- The program declares an array of integers.
- It sorts the array using the Bubble Sort algorithm.
- It prints the sorted array to the console.
The BubbleSort
subroutine repeatedly compares and swaps adjacent elements if they are in the wrong order. The process is repeated until the entire array is sorted.
Feel free to modify the array and observe how the algorithm sorts different sets of numbers.