How to do bubble sorting in vb.net

Written by kevin walker

Share

Tweet

Share

Email

The Bubble Sort is one of the first sorting algorithms learnt by a computer science student, since it is one of the simplest to implement and most intuitive. The basic procedure is simple: starting at the beginning of the array, the program moves through the array looking for an array element that is larger than the one to its right, and swaps them. It then starts over and repeats the same process on the new array. It does this until it can proceed through the entire array without swapping elements. In practice, this makes it look as if the larger elements in the array are bubbles that float to the top of the array.

Skill level:

Easy

Other People Are Reading

Instructions

1

Open Visual Basic and click "File" and "New project" to create a new project. Select "ConsoleApplication." When it comes time to enter your code in a real project with a Graphical User Interface (GUI), you can simply copy this function there without modification.

This generates a simple, unsorted array of integers and tells BubbleSort to sort them, then prints the results.

Tips and warnings

The BubbleSort is one of the simplest sorting algorithms known, however, in most situations, it is also among the slowest. The MergeSort used by the Visual Basic "Sort" command is faster in most circumstances, and easier to implement, since it is part of the .NET library. However, BubbleSort can be faster in some situations, for example, when the list is known in advance to be either sorted or very nearly sorted in advance.