In this tutorial, i will teach you how to create a program for bubble sorting using vb.net console. We all know that bubble sort is a sorting algorithm that is repeatedly searching through lists that need to be sorted, comparing each pair of items and swapping them if they are in the wrong order.
Now, let's start this tutorial!
1. Let's start with creating a Console Application for this tutorial by following the following steps in Microsoft Visual Studio:
Go to File, click
New Project, and choose
Console Application.
2. Name your project as
bubbleSort as your module.
Note: This is a console application so we cannot have visual controls for this tutorial.
3. Add the following code in your module.
Module bubbleSort
Sub Main()
4. Make a function named
sorting. This will automatically sort the inputted elements that we will create in Sub Main.
Sub sorting(ByVal x() As Integer, ByVal y As Integer)
Dim i, a, t As Integer
For i = 0 To y - 1
For a = i + 1 To y - 1
If x(i) > x(a) Then
t = x(i)
x(i) = x(a)
x(a) = t
End If
Next
Next
End Sub
5. For entering number of elements, put this code below.
Console.WriteLine("Bubble Sorting")
Console.WriteLine()
Dim num, i As Integer
Console.Write("Enter Number of Elements: ")
num = CInt(Console.ReadLine)
Dim arr(num) As Integer
Console.WriteLine()
For i = 0 To num - 1
Console.Write("Enter Element(" & (i + 1) & "): ")
arr(i) = CInt(Console.ReadLine)
Next
6. For printing the inputted elements above, put this code below.
Console.WriteLine()
Console.WriteLine("Inputted Elements")
Console.WriteLine()
For i = 0 To num - 1
Console.WriteLine("Element in (" & i & "): " & arr(i))
Next
7. Lastly, we will code for the sorting of elements (bubble sort), put this code below.
Console.WriteLine()
sorting(arr, num)
Console.WriteLine("Sorted Elements")
Console.WriteLine()
For i = 0 To num - 1
Console.WriteLine("Element in (" & i & "): " & arr(i))
Next
Console.ReadLine()
Output:

Hope this program helps! :)
Best Regards,
Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:
[email protected]
Add and Follow me on Facebook:
https://www.facebook.com/donzzsky
Visit and like my page on Facebook at:
https://www.facebook.com/BermzISware