Results 1 to 14 of 14

Thread: [RESOLVED] Bubble Sorting an Array?!?!

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

    Resolved [RESOLVED] Bubble Sorting an Array?!?!

    I am trying to sort my array in ascending&Descending order, BUT im having trouble because the code to enter numbers and the code to sort the array was given to me by my professor but i changed the code to enter numbers into the array, so i think that's what messing my code to sort arrays up because the codes aren't matching up....any help is appreciated
    Code:
    Option Strict On
    
    Public Class Form1
    
        Dim J As Integer = 0
        Dim MyArray As New List(Of Integer)
    
    
        Private Sub btnPutNumberIntoArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPutNumberIntoArray.Click
            Dim number As Integer
            Integer.TryParse(txtNumbers.Text, number)
            If number < 1 Then
                MessageBox.Show("Please enter a number greater then zero.")
            Else
                MyArray.Add(number)
                txtNumbers.Clear()
    
            End If
        End Sub
    
        Private Sub btnDisplayNumbersInArray_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayNumbersInArray.Click
            txtDisplayNumbers.Clear()
            txtDisplayNumbers.Focus()
    
            For J As Integer = 0 To MyArray.Count - 1
                txtDisplayNumbers.Text &= MyArray(J).ToString & Environment.NewLine
            Next
        End Sub
    Private Sub btnDisplayAscendingOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayAscendingOrder.Click
         
            Dim SwapFlag As Boolean
            Dim UpperSub As Integer
            Dim Temp As Integer
            UpperSub = 24
            SwapFlag = True
    
            While SwapFlag = True And UpperSub >= 1
                J = 0
                SwapFlag = False
                While J <= UpperSub - 1
                    If MyArray(J) > MyArray(J + 1) Then
                        Temp = MyArray(J)
                        MyArray(J) = MyArray(J + 1)
                        MyArray(J + 1) = Temp
                        SwapFlag = True
                    End If
                    J = J + 1
                End While
    
                UpperSub = UpperSub - 1
    
            End While
                txtDisplayNumbers.Text = txtDisplayNumbers.Text & CStr(MyArray(J)) & ControlChars.NewLine
    
        End Sub
    
        Private Sub btnDisplayDescendingOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayDescendingOrder.Click
    
            Dim SwapFlag As Boolean
            Dim UpperSub As Integer
            Dim Temp As Integer
            UpperSub = 24
            SwapFlag = True
    
            While SwapFlag = True And UpperSub <= 1
                J = 0
                SwapFlag = False
                While J >= UpperSub - 1
    
                    If MyArray(J) < MyArray(J + 1) Then
                        Temp = MyArray(J)
                        MyArray(J) = MyArray(J + 1)
                        MyArray(J + 1) = Temp
                        SwapFlag = True
                    End If
                    J = J + 1
                End While
                UpperSub = UpperSub - 1
                txtDisplayNumbers.Text = txtDisplayNumbers.Text & CStr(MyArray(J)) & ControlChars.NewLine
    
            End While
    
        End Sub

    At the red Highlight i get this error:
    "Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index"
    Last edited by rosleenXOXO; Dec 14th, 2011 at 10:38 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width