Posts Tagged ‘bubble

10
Apr
09

[VB.Net] Bubble Sort 2

This function displays the steps that are actually used in the algorithm, displaying each iteration and the state of the list at the end of each. This only works with numbers, however could easily be used to sort strings (just cast it to string instead of integer)

    Public Sub BubbleSortOutput(ByVal Delimiter As Char, ByVal NewDelimiter As Char, ByRef inText As String, ByRef outTextBox As TextBox)
        ''Create initial list of numbers from inText
        Dim Numbers As New List(Of Integer)
        ''Format number delimiter number delimiter number etc
        ''E.g. 1,2,3,4,5 or 1.2.3.4.5 or 1-2-3-4-5-6 etc
        ''Split the string and output to text box
        For Each str As String In inText.Split(Delimiter)
            Numbers.Add(CInt(str))
            outTextBox.Text += str & NewDelimiter
        Next
        outTextBox.Text += vbNewLine & "".PadRight(120, "-") & vbNewLine

        ''Starting the iterations
        Dim swaps As Boolean = True
        While swaps = True
            swaps = False
            Dim CurrentPass As List(Of Integer) = Numbers
            For i As Integer = 0 To Numbers.Count - 2
                ''Formatting tabs
                Dim delim As String = "".PadLeft(i, NewDelimiter)
                ''Display numbers that are being compared
                outTextBox.Text += delim & "[" & CurrentPass(i) & NewDelimiter & CurrentPass(i + 1) & "]" & vbNewLine
                ''If left is bigger than right, swap
                If CurrentPass(i) > CurrentPass(i + 1) Then
                    Dim temp As Integer = CurrentPass(i + 1)
                    CurrentPass(i + 1) = CurrentPass(i)
                    CurrentPass(i) = temp
                    swaps = True
                End If
            Next
            'Display output for pass
            outTextBox.Text += "".PadRight(120, "-") & vbNewLine
            For j As Integer = 0 To CurrentPass.Count - 2
                outTextBox.Text += CurrentPass(j) & NewDelimiter
            Next
            outTextBox.Text += CurrentPass(CurrentPass.Count - 1) & vbNewLine
            outTextBox.Text += "".PadRight(120, "-") & vbNewLine
        End While
    End Sub

Usage:

   BubbleSortOutput(","c, vbTab, TextBox1.Text, TextBox2)

Where TextBox1.Text contains: 92,28,48,37,77,34,94,46
and TextBox2 is a blank textbox.

Would output:

92	28	48	37	77	34	94	46
-----------------------------------------------------------
[92	28]
	[92	48]
		[92	37]
			[92	77]
				[92	34]
					[92	94]
						[94	46]
-----------------------------------------------------------
28	48	37	77	34	92	46	94
-----------------------------------------------------------
[28	48]
	[48	37]
		[48	77]
			[77	34]
				[77	92]
					[92	46]
						[92	94]
-----------------------------------------------------------
28	37	48	34	77	46	92	94
-----------------------------------------------------------
[28	37]
	[37	48]
		[48	34]
			[48	77]
				[77	46]
					[77	92]
						[92	94]
-----------------------------------------------------------
28	37	34	48	46	77	92	94
-----------------------------------------------------------
[28	37]
	[37	34]
		[37	48]
			[48	46]
				[48	77]
					[77	92]
						[92	94]
-----------------------------------------------------------
28	34	37	46	48	77	92	94
-----------------------------------------------------------
[28	34]
	[34	37]
		[37	46]
			[46	48]
				[48	77]
					[77	92]
						[92	94]
-----------------------------------------------------------
28	34	37	46	48	77	92	94
-----------------------------------------------------------
Advertisements
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