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. 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.

  2. 2

    Paste the following code above the "Main" function:

    Sub BubbleSort(ByRef arr() As Integer)
    
        Dim temp
    
        Dim switch = True
    
        While switch
    
            switch = False
    
            For x = 0 To arr.Length - 2
    
                    If arr(x) > arr(x+1) Then
    
                        temp = arr(x)
    
                        arr(x) = arr(x+1)
    
                        arr(x+1) = temp
    
                        switch = True
    
                    End If
    
            Next
    
        End While
    
    End Sub
    

    An important thing to recognise is that the arr is passed into the subroutine "ByRef." This allows the function to modify the contents of the array.

  3. 3

    Paste the following into the "Main" function to test the BubbleSort method:

    Sub Main()
    
        Dim arr = {3, 4, 5232, 1, 232, 12, 34, 14, 21, 213, 213, 21, 321}
    
        Console.WriteLine("Unsorted")
    
        For Each x In arr
    
            Console.Write(x & " ")
    
        Next
    
        Console.WriteLine()
    
    
    
        BubbleSort(arr)
    
    
    
        Console.WriteLine("Sorted")
    
        For Each x In arr
    
            Console.Write(x & " ")
    
        Next
    
        Console.ReadKey()
    
    
    
    End Sub
    

    End Module

    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.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.