Let us get right to business. Depending on the application, purpose, and data. These variables will determine what sorting algorithms will be used.

For simplicity sake, we will be using the computer programming language Microsoft Visual Studio – VB.NET available for free from Microsoft.com website.

Application, Purpose, & Data equals sorting algorithm used

So, what is it we are doing?

Examples:

  1. Spy agency looking to use the Internet as a surveillance/communication system.
  2. VoIP & Telecommunications. Sort Network Communication Signals for pirates from the good data users.
  3. Encryption, Decryption.
  4. Sort an array of numbers using the bubble sort algorithm

Again, for simplicities sake let us go with number 4.

Sort an array of numbers using the bubble sort algorithm.

An array of numbers for example on a lottery ticket from Lotto Max 7 main numbers plus a bonus number. Only the first 7 randomly generated numbers are sorted in relation to each other and the last randomly generated number is displayed at the end, excluded from the sort algorithm.

A display of past winning ticket.

The next question, what is the Bubble Sort Algorithm? & Why?

What is the Bubble Sort Algorithm & Why?

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order.

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list, compares adjacent pairs and swaps them if they are in the wrong order.

The pass through the list is repeated until the list is sorted.

The algorithm, which is a comparison sort, is named for the way smaller or larger elements “bubble” to the top of the list.

Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort. Bubble sort can be practical if the input is in mostly sorted order with some out-of-order elements nearly in position.

Example:

First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.

Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.

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 )

Worst and Average Case Time Complexity: O(n*n). The worst case occurs when the array is reverse sorted.

Best Case Time Complexity: O(n). Best case occurs when the array is already sorted.

Auxiliary Space: O(1)

Boundary Cases: Bubble sort takes minimum time (Order of n) when elements are already sorted.

Sorting In Place: Yes

Stable: Yes

Due to its simplicity, bubble sort is often used to introduce the concept of a sorting algorithm.

In computer graphics, it is popular for its capability to detect a very small error (like the swap of just two elements) in almost-sorted arrays and fix it with just linear complexity (2n).

For example, it is used in a polygon filling algorithm, where bounding lines are sorted by their x coordinate at a specific scan line (a line parallel to the x-axis) and with incrementing y their order changes (two elements are swapped) only at intersections of two lines.

So why the bubble sort?

The bubble sort for this particular case is for a small set of numbers and it is very practical and easy to do as our number set is only 7 numbers to be sorted.

 

The VB.NET code for the Bubble sort algorithm

Before we get started let us download & install Visual Studios Community Edition. Click here to download Visual Studios Community Edition.

If you would rather not install Microsoft Visual Studios Community Edition for free on your computer and would just rather compile online, remember when using the online compiler you cannot make Windows Forms SDK programs. With the online compiler, you can only compile console like applications. Try JDoodle for online VB.NET compiling.

Module Module1
Sub Main()
        
   'this is the list of random numbers from the lottery image
   'shown above. I have scrambled the numbers order for
   'presentation purposes.
   Dim arr() As Integer = New Integer() {39, 24, 49, 9, 10, 16, 23}

   'sort the array using bubble sort
    bubbleSort(arr, arr.Length)

   'output the sorted array
    Dim i As Integer

    For i = 0 To arr.Length - 1
          Console.WriteLine(arr(i))
    Next

    Console.ReadLine() 'wait for keypress
End Sub

Sub bubbleSort(ByVal dataset() As Integer, ByVal n As Integer)

     Dim i, j As Integer

     For i = 0 To n Step 1
            For j = n - 1 To i + 1 Step -1
                If (dataset(j) < dataset(j - 1)) Then 
                    Dim temp As Integer = dataset(j)
                    dataset(j) = dataset(j - 1)
                    dataset(j - 1) = temp 
                End If 
            Next
      Next
End Sub

End Module 

Below is an embedded Jdoodle VB.NET Console Application of the Bubble sort algorithm code shown above. Feel free to change the numbers around and see how the algorithm is able to sort the numbers every time in less than 1 second.

If you used Microsoft Visual Studios Visual Basic.NET you should get similar results as the output shown in the image below.

Microsoft Visual Studios VB.NET Bubble Sort Output.

Microsoft Visual Studios VB.NET Bubble Sort Output.

Code Review

We will walk through the code line by line explaining what each line does respectively… To be continued…