REM Written By Daniel R. Spohn 'Homework 2- Sorting 'January 23, 2004 Option Strict On Public Class Sort Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents txtOriginal As System.Windows.Forms.TextBox Friend WithEvents txtSorted As System.Windows.Forms.TextBox Friend WithEvents btnSort As System.Windows.Forms.Button Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents mnuFile As System.Windows.Forms.MenuItem Friend WithEvents mnuFileExit As System.Windows.Forms.MenuItem Private Sub InitializeComponent() Me.txtOriginal = New System.Windows.Forms.TextBox() Me.txtSorted = New System.Windows.Forms.TextBox() Me.MainMenu1 = New System.Windows.Forms.MainMenu() Me.mnuFile = New System.Windows.Forms.MenuItem() Me.mnuFileExit = New System.Windows.Forms.MenuItem() Me.btnSort = New System.Windows.Forms.Button() Me.Label1 = New System.Windows.Forms.Label() Me.Label2 = New System.Windows.Forms.Label() Me.SuspendLayout() ' 'txtOriginal ' Me.txtOriginal.Location = New System.Drawing.Point(24, 16) Me.txtOriginal.Multiline = True Me.txtOriginal.Name = "txtOriginal" Me.txtOriginal.ReadOnly = True Me.txtOriginal.ScrollBars = System.Windows.Forms.ScrollBars.Vertical Me.txtOriginal.Size = New System.Drawing.Size(192, 448) Me.txtOriginal.TabIndex = 0 Me.txtOriginal.Text = "" ' 'txtSorted ' Me.txtSorted.Location = New System.Drawing.Point(248, 16) Me.txtSorted.Multiline = True Me.txtSorted.Name = "txtSorted" Me.txtSorted.ReadOnly = True Me.txtSorted.ScrollBars = System.Windows.Forms.ScrollBars.Vertical Me.txtSorted.Size = New System.Drawing.Size(192, 448) Me.txtSorted.TabIndex = 1 Me.txtSorted.Text = "" ' 'MainMenu1 ' Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile}) ' 'mnuFile ' Me.mnuFile.Index = 0 Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFileExit}) Me.mnuFile.Text = "&File" ' 'mnuFileExit ' Me.mnuFileExit.Index = 0 Me.mnuFileExit.Text = "E&xit" ' 'btnSort ' Me.btnSort.Location = New System.Drawing.Point(184, 472) Me.btnSort.Name = "btnSort" Me.btnSort.Size = New System.Drawing.Size(104, 32) Me.btnSort.TabIndex = 2 Me.btnSort.Text = "Sort" ' 'Label1 ' Me.Label1.Location = New System.Drawing.Point(32, 0) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(168, 16) Me.Label1.TabIndex = 3 Me.Label1.Text = "Randomly Generated List" Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'Label2 ' Me.Label2.Location = New System.Drawing.Point(256, 0) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(168, 16) Me.Label2.TabIndex = 4 Me.Label2.Text = "Sorted List" Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'Sort ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(464, 505) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label2, Me.Label1, Me.btnSort, Me.txtSorted, Me.txtOriginal}) Me.Menu = Me.MainMenu1 Me.Name = "Sort" Me.Text = "Sort" Me.ResumeLayout(False) End Sub #End Region #Region "Declarations" 'Declares an array to store the randomly generated list Dim mintOriginalList() As Integer #End Region #Region "Subs and Functions" Private Sub GenerateNumbers(ByVal intAmount As Integer) 'This sub randomly generates numbers Dim i As Integer 'between -5000 and 10000 Dim intOriginalList(intAmount - 1) As Integer For i = 1 To intAmount intOriginalList(i - 1) = CInt(Math.Floor(Rnd() * (((15001)))) - 5000) Next mintOriginalList = intOriginalList End Sub Private Sub NumberInput() 'This sub takes input from the user, about how many numbers should be generated Dim intTotalNumbers As Integer Dim strInput As String Do strInput = (InputBox("Enter the number of values you want, between 2 and 10000", "")) Loop Until IsNumeric(strInput) And Val(strInput) >= 2 And Val(strInput) <= 10000 intTotalNumbers = CInt(strInput) GenerateNumbers(intTotalNumbers) DisplayNumbers() End Sub Private Sub DisplayNumbers() 'This sub displays the original list in the listbox Dim i As Integer txtOriginal.Text = "" Dim sb As New System.Text.StringBuilder() 'Using string builder is much faster then concatenating strings For Each i In mintOriginalList sb.Append(CStr(i)) sb.Append(vbCrLf) Next txtOriginal.Text = sb.ToString End Sub Private Sub Sort() 'This Sub will copy the original list and then sort it Dim intCounter As Integer Dim intSortedList(mintOriginalList.Length - 1) As Integer For intCounter = 0 To mintOriginalList.Length - 1 'Copies the original list to the new list intSortedList(intCounter) = mintOriginalList(intCounter) Next Dim intComparableNumber As Integer Dim intNumberOfPasses As Integer = mintOriginalList.Length + 1 Dim intLengthOfPass As Integer = 1 Dim blnDone As Boolean = False Dim i As Integer Do Until intNumberOfPasses = intLengthOfPass + 1 i = intLengthOfPass intComparableNumber = intSortedList(i) Do Until (blnDone = True) Or (i = 0) 'This implements insertion sort If intComparableNumber < intSortedList(i - 1) Then intSortedList(i) = intSortedList(i - 1) i -= 1 If i = 0 Then intSortedList(i) = intComparableNumber End If Else intSortedList(i) = intComparableNumber blnDone = True End If Loop blnDone = False intLengthOfPass += 1 Loop Dim p As Integer txtSorted.Text = "" 'declares a new stringbuilder and will build the new ordered list and will add it to the text box Dim sb As New System.Text.StringBuilder() For Each p In intSortedList sb.Append(CStr(p)) sb.Append(vbCrLf) Next txtSorted.Text = sb.ToString 'If the after sort checks work, the user is informed. if they dont work they are also informed If (CheckSortOrder(intSortedList, intSortedList.Length)) = True AndAlso _ (CheckList(intSortedList, intSortedList.Length)) = True Then MessageBox.Show("Sort Worked") Else MessageBox.Show("Sort Did Not Work") End If End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Randomize() NumberInput() End Sub Private Function CheckSortOrder(ByVal intSorted() As Integer, ByVal intSortedLength As Integer) As Boolean 'This check, compares a number to the next number and continues throughout the list. if the check completes then it is in order Dim i As Integer For i = 0 To intSortedLength - 2 If intSorted(i) > intSorted(i + 1) Then Return False End If Next Return True End Function Private Function CheckList(ByVal intSortedList() As Integer, ByVal intSortedLength As Integer) As Boolean 'this sub creates to arrays with a value for each possible number. for example: the number -5000 'would be 0 and 5,000 would be 10000. when it comes across a number, it adds 1 to that index, once this is ' completed for all the values the arrays are compared, if they are the same then, they contain the same numbers. Dim intTotalOriginal(15000) As Integer Dim intTotalSorted(15000) As Integer Dim i As Integer = 0 For i = 0 To mintOriginalList.Length - 1 Dim intNumber As Integer = 0 intNumber = mintOriginalList(i) intNumber += 5000 intTotalOriginal(intNumber) += 1 Next i = 0 For i = 0 To intSortedLength - 1 Dim intNumber As Integer = 0 intNumber = intSortedList(i) intNumber += 5000 intTotalSorted(intNumber) += 1 Next i = 0 Do Until intTotalOriginal(i) <> intTotalSorted(i) i += 1 If i = 15000 Then If intTotalOriginal(i) = intTotalSorted(i) Then Return True End If End If Loop End Function #End Region #Region "Buttons and MenuItems" Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click Sort() End Sub Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click Me.Close() End Sub #End Region End Class