Posts Tagged ‘number

10
Apr
09

[Vb.Net] QuickSort

This uses the QuickSort algorithm to order a list of numbers:

  Private Function QuickSort(ByVal ListOfNumbers As List(Of Integer)) As List(Of Integer)
        ''If the list is 1 or 0 then it is sorted
        If ListOfNumbers.Count < 1 Then Return ListOfNumbers
        ''List of numbers smaller than the pivot, and larger than it
        Dim SmallList, LargeList As New List(Of Integer)
        ''Pivot = n/2 rounded down
        Dim Pivot As Integer = ListOfNumbers(Math.Floor((ListOfNumbers.Count) / 2))
        Dim PivotCount As Integer = 0
        QuickSort = New List(Of Integer)
        ''Add to appropriate list
        For i As Integer = 0 To ListOfNumbers.Count - 1
            If ListOfNumbers(i) < Pivot Then
                SmallList.Add(ListOfNumbers(i))
            ElseIf ListOfNumbers(i) > Pivot Then
                LargeList.Add(ListOfNumbers(i))
            Else
                PivotCount = PivotCount + 1
            End If
        Next
        ''Quicksort smaller list
        QuickSort.AddRange(QuickSort(SmallList))
        ''Add pivot
        While PivotCount > 0
            QuickSort.Add(Pivot)
            PivotCount -= 1
        End While
        ''Sort large list 
        QuickSort.AddRange(QuickSort(LargeList))
    End Function

Usage:

        Dim ArrayOfNumbers As Integer() = {21, 18, 16, 20, 11, 17, 15, 7, 3, 18, 19, 14, 11, 28, 12, 15, 7, 30, 27}
        Dim ListOfNumbers As New List(Of Integer)
        ListOfNumbers.AddRange(ArrayOfNumbers)
        For Each Number As Integer In QuickSort(ListOfNumbers)
            Debug.Write(Number & " ")
        Next
        Debug.Write(vbNewLine)

Would Output:
3 7 7 11 11 12 14 15 15 16 17 18 18 19 20 21 27 28 30

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

01
Apr
09

[Javascript] Number Format String (2)

Second one, similar to the last but instead of reversing twice, it builds the string backwards then reverses it.

function numberFormat_2(text) {
	//Loop backwards through the string
	//Add every character
	//If index = 3, add comma
	//Reverse string;
	var i = text.length;
	var output = "";
	var k=0;
	var j=-1;
	while (i > -1) {
		output += text.charAt(i);
		j++;
		if(j==3 && i !=0) {
			output += ",";
			j=0;
		}
		i--;
	}
	output = output.split("").reverse().join("");
	return output
}

Usage:

	var text = "123456789123456789";
	text = numberFormat_2(text);
	alert(text);

Would Output:

123,456,789,123,456,789

31
Mar
09

[Javascript] Number Format String (1)

Ive been set a mission to make 3, different, functions to format a number with commas, e.g. 12345 = 12,345; 1234567890 = 1,234,567,890

Here is number one:

function numberFormat_1(text) {
	//Reverse string
	//Loop through adding every character
	//If index = 3, add a comma
	//Reverse string
	text = text.split("").reverse().join("");
	var output = "";
	var j=0;
	for(var i = 0;i<text.length;i++) {
		output += text.charAt(i);
		j++;
		if(j==3 && i != text.length-1) {
			output += ",";
			j=0;
		}
	}
	output = output.split("").reverse().join("");
	return output;
}

Usage:

	var text = "123456789123456789";
	text = numberFormat_1(text);
	alert(text);

Would Output:

123,456,789,123,456,789