10
Apr
09

[VB.Net] Bubble Sort Algorithm

Two functions below, for using the Bubble sort algorithm on either Strings or Integers.
The two functions are fundamentally the same, just with different casting – and so could be combined into one function.

    Public Function BubbleSortNumber(ByVal ListofNumber As List(Of Integer)) As List(Of Integer)
        Dim swaps As Boolean = True
        While swaps = True
            swaps = False
            For i As Integer = 0 To ListofNumber.Count - 2
                If ListofNumber(i) > ListofNumber(i + 1) Then
                    Dim temp As Integer = ListofNumber(i + 1)
                    ListofNumber(i + 1) = ListofNumber(i)
                    ListofNumber(i) = temp
                    swaps = True
                End If
            Next
        End While
        Return ListofNumber
    End Function

Usage:

        ''Create List of Random Integers
        Dim randomG As New Random
        Dim ListOfInteger As New List(Of Integer)
        For i As Integer = 0 To 26
            ListOfInteger.Add(randomG.Next(0, 10))
        Next
        ''Sort and format
        Dim outpt2 As String = ""
        For Each bub As String In BubbleSortNumber(ListOfInteger)
            outpt2 += bub & " "
        Next
        Debug.WriteLine(outpt2)

Would output:
0 0 0 0 1 2 3 3 3 3 3 4 5 5 5 6 6 6 6 7 7 8 8 8 8 8 9

And for Strings

    Public Function BubbleSortString(ByVal ListofString As List(Of String)) As List(Of String)
        Dim swaps As Boolean = True
        While swaps = True
            swaps = False
            For i As Integer = 0 To ListofString.Count - 2
                If ListofString(i) > ListofString(i + 1) Then
                    Dim temp As String = ListofString(i + 1)
                    ListofString(i + 1) = ListofString(i)
                    ListofString(i) = temp
                    swaps = True
                End If
            Next
        End While
        Return ListofString
End Function

Usage:

        ''Create list of strings (in this case, characters)
        Dim ListOfString As New List(Of String)
        ListOfString.AddRange("q w e r t y u i o p a s d f g h j k l z x c v b n m".Split(" "))
        ''Sort and format
        Dim outpt1 As String = ""
        For Each bub As String In BubbleSortString(ListOfString)
            outpt1 += bub & " "
        Next
        Debug.WriteLine(outpt1)

Would output:
a b c d e f g h i j k l m n o p q r s t u v w x y z

Advertisements

0 Responses to “[VB.Net] Bubble Sort Algorithm”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: