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

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



  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: