In Feburary 2016 18,
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.
Paste the following code above the 'Main' function:
Sub BubbleSort(ByRef arr() As Integer)Dim tempDim switch = TrueWhile switchswitch = FalseFor x = 0 To arr.Length - 2If arr(x) > arr(x+1) Thentemp = arr(x)arr(x) = arr(x+1)arr(x+1) = tempswitch = TrueEnd IfNextEnd WhileEnd Sub
An important thing to recognize is that the arr is passed into the subroutine 'ByRef.' This allows the function to modify the contents of the array.
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 arrConsole.Write(x & ' ')NextConsole.WriteLine()BubbleSort(arr)Console.WriteLine('Sorted')For Each x In arrConsole.Write(x & ' ')NextConsole.ReadKey()End Sub
End ModuleThis generates a simple, unsorted array of integers and tells BubbleSort to sort them, then prints the results.
In Feburary 2016 18,
No comments:
Post a Comment